25.3 Allowing Custom Attributes to Be Applied Multiple Times

 <  Day Day Up  >  

You want to specify that an attribute can be applied more than once to the same item.


Technique

You should specify the AllowMultiple property of the AttributeUsage attribute, as shown in Listing 25.9.

Listing 25.9 Add a Title Here
 [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class MyMultipleAttribute : Attribute {         // etc. 

With the definition of MyMultipleAttribute in Listing 25.9, and with the additional assumption that MyMultipleAttribute has a constructor that takes a string, you can use MyMultipleAttribute as shown in Listing 25.10.

Listing 25.10 Using an Attribute More Than Once on the Same Target
 [MyMultipleAttribute("This is some text")] [MyMultipleAttribute("Hey, this is some more text!")] public override void SomeMethod() {         // etc. 

If we had not specified AllowMultiple=true in Listing 25.9, then Listing 25.10 would have resulted in a compilation error, because the default behavior is that a given attribute cannot be applied more than once to the same target.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net