[ Team LiB ] |
14.5 Defining a New Custom AttributeIn addition to using the predefined attributes supplied by the .NET Framework, you can also create your own. To create a custom attribute, use the following steps:
Consider the following example of a custom attribute, CrossRefAttribute, which removes the limitation that the CLR metadata contains only information about statically linked types, but not dynamically linked ones. // Xref.cs - cross-reference custom attribute // compile with: // csc /t:library XRef.cs using System; [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class CrossRefAttribute : Attribute { Type xref; string desc = ""; public string Description { set { desc=value; } } public CrossRefAttribute(Type xref) { this.xref=xref; } public override string ToString( ) { string tmp = (desc.Length>0) ? " ("+desc+")" : ""; return "CrossRef to "+xref.ToString( )+tmp; } } From the attribute user's perspective, this attribute can be applied to any target multiple times (note the use of the AttributeUsage attribute to control this). CrossRefAttribute takes one mandatory positional parameter (namely the type to cross reference) and one optional named parameter (the description), and is used as follows: [CrossRef(typeof(Bar), Description="Foos often hang around Bars")] class Foo {...} Essentially, this attribute embeds cross references to dynamically linked types (with optional descriptions) in the metadata. This information can then be retrieved at runtime by a class browser to present a more complete view of a type's dependencies. |
[ Team LiB ] |