Type.GetCustomAttributes
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class AuthorAttribute : System.Attribute
{
private string company;
private string name;
public AuthorAttribute(string name)
{
this.name = name;
company = "";
}
public string Company
{
get { return company; }
set { company = value; }
}
public string Name
{
get { return name; }
}
}
[assembly: Author("Tom", Company = "Ltd.")]
[Author("Tom", Company = "Abc Ltd.")]
class SomeClass { }
[Author("Lena")]
public class SomeOtherClass
{
}
[Author("FirstName")]
[Author("Jack", 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);
}
}
}
Related examples in the same category