Mega Code Archive

 
Categories / C# Tutorial / Attribute
 

Create Attribute

using System; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class AuthorAttribute : System.Attribute {     public string Company;      public string Name;         public AuthorAttribute(string name)     {         this.Name = name;         Company = "";     } } [Author("Name1")] [Author("Name2", Company = "Ltd.")] class MainClass {     public static void Main()     {         Type type = typeof(MainClass);         object[] attrs = type.GetCustomAttributes(typeof(AuthorAttribute), true);         foreach (AuthorAttribute a in attrs)         {             Console.WriteLine(a.Name + ", " + a.Company);         }     } } Name2, Ltd. Name1,