C# 如何创建重复的允许属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/553540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create duplicate allowed attributes
提问by ebattulga
I'm using a custom attribute inherited from an attribute class. I'm using it like this:
我正在使用从属性类继承的自定义属性。我是这样使用它的:
[MyCustomAttribute("CONTROL")]
[MyCustomAttribute("ALT")]
[MyCustomAttribute("SHIFT")]
[MyCustomAttribute("D")]
public void setColor()
{
}
But the "Duplicate 'MyCustomAttribute' attribute" error is shown.
How can I create a duplicate allowed attribute?
但是显示了“重复的‘MyCustomAttribute’属性”错误。
如何创建重复的允许属性?
采纳答案by Anton Gogolev
Stick a AttributeUsage
attribute onto your Attribute class (yep, that's mouthful) and set AllowMultiple
to true
:
将一个AttributeUsage
属性粘贴到您的 Attribute 类上(是的,这是一口)并设置AllowMultiple
为true
:
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class MyCustomAttribute: Attribute
回答by Marc Gravell
AttributeUsageAttribute ;-p
AttributeUsageAttribute ;-p
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class MyAttribute : Attribute
{}
Note, however, that if you are using ComponentModel (TypeDescriptor
), it only supports one attribute instance (per attribute type) per member; raw reflection supports any number...
但是请注意,如果您使用 ComponentModel( TypeDescriptor
),则它仅支持每个成员一个属性实例(每个属性类型);原始反射支持任何数字...
回答by tvanfosson
As an alternative, think about redesigning your attribute to allow for a sequence.
作为替代方案,考虑重新设计您的属性以允许序列。
[MyCustomAttribute(Sequence="CONTROL,ALT,SHIFT,D")]
or
或者
[MyCustomAttribute("CONTROL-ALT-SHIFT-D")]
then parse the values to configure your attribute.
然后解析值以配置您的属性。
For an example of this check out the AuthorizeAttribute in ASP.NET MVC source code at www.codeplex.com/aspnet.
有关此示例,请查看www.codeplex.com/aspnet 上的ASP.NET MVC 源代码中的 AuthorizeAttribute 。
回答by mcdrewski
Anton's solutionis correct, but there is another gotcha.
Anton 的解决方案是正确的,但还有另一个问题。
In short, unless your custom attrbiute overrides TypeId, then accessing it through PropertyDescriptor.GetCustomAttributes()
will only return a single instance of your attribute.
简而言之,除非您的自定义属性覆盖 TypeId,否则通过访问它PropertyDescriptor.GetCustomAttributes()
只会返回您的属性的单个实例。
回答by Axle
After you add the AttributeUsage, make sure you add this property to your Attribute class
添加 AttributeUsage 后,请确保将此属性添加到 Attribute 类
public override object TypeId
{
get
{
return this;
}
}
回答by Ian Kemp
By default, Attribute
s are limited to being applied only once to a single field/property/etc. You can see this from the definition of the Attribute
class on MSDN:
默认情况下,Attribute
s 仅限于仅应用于单个字段/属性/等。您可以从MSDN 上类的定义中Attribute
看到这一点:
[AttributeUsageAttribute(..., AllowMultiple = false)]
public abstract class Attribute : _Attribute
Therefore, as others have noted, all subclasses are limited in the same way, and should you require multiple instances of the same attribute, you need to explicitly set AllowMultiple
to true
:
因此,正如其他人所指出的,所有子类都以相同的方式受到限制,如果您需要同一属性的多个实例,则需要显式设置AllowMultiple
为true
:
[AttributeUsage(..., AllowMultiple = true)]
public class MyCustomAttribute : Attribute
On attributes that allow multiple usages, you should also override the TypeId
propertyto ensure that properties such as PropertyDescriptor.Attributes
work as expected. The easiest way to do this is to implement that property to return the attribute instance itself:
在允许多次使用的属性上,您还应该覆盖该TypeId
属性以确保诸如此类的属性PropertyDescriptor.Attributes
按预期工作。最简单的方法是实现该属性以返回属性实例本身:
[AttributeUsage(..., AllowMultiple = true)]
public class MyCustomAttribute : Attribute
{
public override object TypeId
{
get
{
return this;
}
}
}
(Posting this answer not because the others are wrong, but because this is a more comprehensive/canonical answer.)
(发布这个答案不是因为其他人错了,而是因为这是一个更全面/规范的答案。)