In C #, you define a member of a class, and you can define a property called an attribute. Attribute is called an attribute.
In the FCL, there are built-in attribute. such as:
Condition [Attribute]: In what conditions can be called. (can only be used on methods that return a value of void)
Obsolete: Method deprecated. Support disabled.
Code Listing 1:
classProgram {Static voidMain (string[] args) {Func (); Console.ReadLine (); } [Obsolete ("You can use Func -to-replace this",true)] Private Static voidTest () {Console.WriteLine ("noooo"); } Private Static voidFunc () {Console.WriteLine ("yesss"); } } classT {[Conditional ("DEBUG")] Public Static voidMstringstr) {Console.WriteLine ("method{0}", str); } }
built-in attribute
custom Attribute:
Code Listing 2:
[AttributeUsage (attributetargets.class, AllowMultiple =false, inherited =false)] Public classHelpattribute:attribute { PublicHelpAttribute (stringstr) {Helpstr=str; } Public stringName {Get;Set; } Public stringHELPSTR {Get;Set; } }
Custom Attribute
AttributeTargets.Class: The action target can only be a class.
AllowMultiple: Whether the attribute can be multiple-acting on an element.
Inherited: Whether the attribute also inherits when the target is inherited.
Reflection Gets the attribute information for the class :
Code Listing 3:
classProgram {Static voidMain (string[] args) { varAttrs =typeof(MyClass). GetCustomAttributes (false); for(inti =0; I < Attrs. Length; i++) { varattr = Attrs[i] asHelpAttribute; if(attr! =NULL) Console.WriteLine (attr. Helpstr +attr. Name); } console.readline (); }} [Help ("Good", Name ="class")] classMyClass {}
Reflection Get Property information
The attribute of C # syntax