Mega Code Archive

 
Categories / C# Tutorial / Attribute
 

Assembly level attributes dont have to be in the assemblyinfo cs file, but must be lised outside of any namespae definition

using System; [assembly:System.CLSCompliantAttribute(true)] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class MyDescriptionAttribute : System.Attribute {   private string description;   public string Desc   {     get { return description; }     set { description = value; }   }   public MyDescriptionAttribute() {}   public MyDescriptionAttribute(string desc)    { description = desc;} } [MyDescriptionAttribute("Info")] public class MyClass {     public MyClass()     {     } } public class MainClass {   public static int Main(string[] args)   {     Type t = typeof(MyClass);        // Get all attributes in the assembly.     object[] customAtts = t.GetCustomAttributes(false);        // List all info.     Console.WriteLine("Value of MyDescriptionAttribute");     foreach(MyDescriptionAttribute v in customAtts)       Console.WriteLine("-> {0}\n", v.Desc);       return 0;   } } Value of MyDescriptionAttribute - Info