How to create duplicate allowed attributes

ebattulga picture ebattulga · Feb 16, 2009 · Viewed 32.6k times · Source

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?

Answer

Anton Gogolev picture Anton Gogolev · Feb 16, 2009

Stick a 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