Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Attribute.GetCustomAttributes Method (Assembly, Type)

Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, and the type of the custom attribute to search for.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static Attribute[] GetCustomAttributes (
	Assembly element,
	Type attributeType
)
public static Attribute[] GetCustomAttributes (
	Assembly element, 
	Type attributeType
)
public static function GetCustomAttributes (
	element : Assembly, 
	attributeType : Type
) : Attribute[]

Parameters

element

An object derived from the Assembly class that describes a reusable collection of modules.

attributeType

The type, or a base type, of the custom attribute to search for.

Return Value

An Attribute array that contains the custom attributes of type attributeType applied to element, or an empty array if no such custom attributes exist.

Exception typeCondition

ArgumentNullException

element or attributeType is a null reference (Nothing in Visual Basic).

ArgumentException

attributeType is not derived from Attribute.

The following code example demonstrates the use of GetCustomAttributes, taking an Assembly as a parameter.

using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

namespace CustAttrs1CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Assembly type to access its metadata.
            Assembly assy = clsType.Assembly;

            // Iterate through the attributes for the assembly.
            foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
                // Check for the AssemblyTitle attribute.
                if (attr.GetType() == typeof(AssemblyTitleAttribute))
                    Console.WriteLine("Assembly title is \"{0}\".",
                        ((AssemblyTitleAttribute)attr).Title);

                // Check for the AssemblyDescription attribute.
                else if (attr.GetType() == 
                    typeof(AssemblyDescriptionAttribute))
                    Console.WriteLine("Assembly description is \"{0}\".",
                        ((AssemblyDescriptionAttribute)attr).Description);

                // Check for the AssemblyCompany attribute.
                else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                    Console.WriteLine("Assembly company is {0}.",
                        ((AssemblyCompanyAttribute)attr).Company);
            }
        }
    }
}

/*
 * Output:
 * Assembly company is Microsoft.
 * Assembly description is "GetCustomAttributes() Demo".
 * Assembly title is "CustAttrs1CS".
 */

package CustAttrs1JSL; 
import System.*;
import System.Reflection.*;
import System.Collections.*;

/** @assembly AssemblyTitle("CustAttrs1JSL")
 */
/** @assembly AssemblyDescription("GetCustomAttributes() Demo")
 */
/** @assembly AssemblyCompany("Microsoft")
 */
class DemoClass
{
    public static void main(String[] args)
    {
        Type clsType = DemoClass.class.ToType();

        // Get the Assembly type to access its metadata.
        Assembly assy = clsType.get_Assembly();

        // Iterate through the attributes for the assembly.
        IEnumerator objEnum = Attribute.GetCustomAttributes(assy).
            GetEnumerator();

        while (objEnum.MoveNext()) {
            Attribute attr = (Attribute)objEnum.get_Current();

            // Check for the AssemblyTitle attribute.
            if (attr.GetType().Equals(AssemblyTitleAttribute.class.ToType())) {
                Console.WriteLine("Assembly title is \"{0}\".", 
                    ((AssemblyTitleAttribute)(attr)).get_Title());
            }
            // Check for the AssemblyDescription attribute.
            else {
                if (attr.GetType().Equals(AssemblyDescriptionAttribute.class.
                    ToType())) {
                    Console.WriteLine("Assembly description is \"{0}\".",
                        ((AssemblyDescriptionAttribute)(attr)).
                        get_Description());
                }
                // Check for the AssemblyCompany attribute.
                else {
                    if (attr.GetType().Equals(AssemblyCompanyAttribute.class.
                        ToType())) {
                        Console.WriteLine("Assembly company is {0}.", 
                            ((AssemblyCompanyAttribute)(attr)).get_Company());
                    }
                }
            }
        }
    } //main
} //DemoClass
   
/*
    Output:
    Assembly company is Microsoft.
    Assembly description is "GetCustomAttributes() Demo".
    Assembly title is "CustAttrs1JSL".
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft