How to create duplicate allowed attributes

C#Attributes

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Stick an AttributeUsage attribute onto your Attribute class (yep, that's mouthful) and set AllowMultiple to true:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class MyCustomAttribute: Attribute

Solution 2 - C#

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...

Solution 3 - C#

Anton's solution is correct, but there is another gotcha.

In short, unless your custom attribute overrides TypeId, then accessing it through PropertyDescriptor.GetCustomAttributes() will only return a single instance of your attribute.

Solution 4 - C#

By default, Attributes 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:

[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:

[AttributeUsage(..., AllowMultiple = true)]
public class MyCustomAttribute : Attribute

On attributes that allow multiple usages, you should also override the TypeId property to 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:

[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.)

Solution 5 - C#

After you add the AttributeUsage, make sure you add this property to your Attribute class

public override object TypeId
{
  get
  {
    return this;
  }
}

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionebattulgaView Question on Stackoverflow
Solution 1 - C#Anton GogolevView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow
Solution 3 - C#mcdrewskiView Answer on Stackoverflow
Solution 4 - C#Ian KempView Answer on Stackoverflow
Solution 5 - C#AxleView Answer on Stackoverflow
Solution 6 - C#tvanfossonView Answer on Stackoverflow