Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would one want to use AttributeUsage AllowMultiple when creating attributes?

According to a book I'm reading, the AllowMultiple public property of AttributeUsage specifies:

...whether the target can have multiple instances of the attribute applied to it.

Why would I want/not want to use this?

like image 352
Scott Davies Avatar asked Jan 07 '10 01:01

Scott Davies


People also ask

What is the use of the AttributeUsage attribute?

The AttributeUsage attribute determines how a custom attribute class can be used. AttributeUsageAttribute is an attribute you apply to custom attribute definitions. The AttributeUsage attribute enables you to control: Which program elements attribute may be applied to.

What is AttributeUsage C#?

Determines how a custom attribute class can be used. AttributeUsage is an attribute that can be applied to custom attribute definitions to control how the new attribute can be applied. So it basically gives the compiler some extra information about the attribute class you will implement.

How do I add custom attributes to my framework?

AttributeUsage has a named parameter, AllowMultiple , with which you can make a custom attribute single-use or multiuse. In the following code example, a multiuse attribute is created. In the following code example, multiple attributes of the same type are applied to a class. [Author("P.


3 Answers

Attributes are meta-data. Typically, you'll want to decorate a member or type with an Attribute in order to track some information about it.

For example, the DescriptionAttribute is used by the PropertyGrid to label a description of a property:

[Description("This is my property")]
public int MyProperty { get; set; }

Most of the time, having more than one description would not make sense.

However, it is possible that a specific attribute makes sense to use more than once. In that case, you'd want to set the Attribute to allow multiple instances of itself tagged to the same attribute.

(Not that I'd do this, but...) Say you made a custom attribute to track major changes to a class. You might want to list this for every major change:

[Changes(Version=1.1, Change="Added Foo Feature")]
[Changes(Version=2.0, Change="Added Bar Feature")]
public class MyClass
{
    // ...
like image 186
Reed Copsey Avatar answered Oct 01 '22 10:10

Reed Copsey


This example might be a little contrived but hopefully it gets the point across.

[Convertable(typeof(Int32)), Convertable(typeof(Double))]
public class Test
{

}
like image 45
ChaosPandion Avatar answered Oct 01 '22 11:10

ChaosPandion


This depends what the attributes are.

For example, you could make an attribute that marks a class as depending on something, and you could allow multiple dependencies.

For a concrete example, look at SuppressMessage, which suppresses a code analysis warning. A member can have multiple warnings that you might want to suppress.

Another example is WebResource; an assembly can contain multiple resources.

like image 21
SLaks Avatar answered Oct 01 '22 11:10

SLaks