usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.IO;namespacecodetest{classProgram {Static voidMain (string[] args) { //get information from attribute by reflectionMyAttribute myattribute; foreach(varattrinch typeof(MyClass). GetCustomAttributes (true) ) {MyAttribute= attr asMyAttribute; Console.WriteLine (MyAttribute. Name); } console.readline (); } } //Custom attribute are often used with AttributeUsage to limit the use of custom attribute[AttributeUsage (attributetargets.class, AllowMultiple =false, inherited =false)] classMyattribute:attribute {//myattribute must inherit attribute stringname; PublicMyAttribute (stringNamein) { This. Name =Namein; } Public stringName {Get { return This. Name; } } Public stringdesc {Get; Set; } } //MyAttribute can be abbreviated to My[My ("MyAttribute", desc="MyClass")] classMyClass {}}
attribute in C #