CSharp examples for Reflection:Attribute
Inspect the Attributes of a Program Element Using Reflection
using System;/* ww w . j a va 2s .c om*/ [Author("Lena")] [Author("Allen", Company = "Apress")] 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); } } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class AuthorAttribute : System.Attribute { private string company; // Creator's company private string name; // Creator's name // Declare a public constructor. public AuthorAttribute(string name) { this.name = name; company = ""; } // Declare a property to get/set the company field. public string Company { get { return company; } set { company = value; } } // Declare a property to get the internal field. public string Name { get { return name; } } }