Type.GetCustomAttributes : Type « System « C# / C Sharp by API






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

1.Type.BaseType
2.Type.ContainsGenericParameters
3.Type.FullName
4.Type.GetConstructor
5.Type.GetEvents()
6.Type.GetFields()
7.Type.GetGenericArguments()
8.Type.GetGenericTypeDefinition()
9.Type.GetInterfaces()
10.Type.GetMethod(String methodName);
11.Type.GetMethods()
12.Type.GetProperties()
13.Type.GetType(String typeName)
14.Type.GetType(String typeName, true)
15.Type.GetType(String typeName, true, true);
16.Type.IsAbstract
17.Type.IsClass
18.Type.IsCOMObject
19.Type.IsEnum
20.Type.IsGenericTypeDefinition
21.Type.IsSealed
22.Type.MakeGenericType
23.Type.Name
24.Type.ReflectionOnlyGetType
25.Type.UnderlyingSystemType