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 (Module, Type)

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

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

public static Attribute[] GetCustomAttributes(
	Module element,
	Type attributeType
)

Parameters

element
Type: System.Reflection.Module

An object derived from the Module class that describes a portable executable file.

attributeType
Type: System.Type

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

Return Value

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

ExceptionCondition
ArgumentNullException

element or attributeType is null.

ArgumentException

attributeType is not derived from Attribute.

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

using System;
using System.Reflection;
using System.ComponentModel;

// Assign some attributes to the module.
[module:Description("A sample description")]

// Set the module's CLSCompliant attribute to false 
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]

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

            // Iterate through all the attributes for the module. 
            foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
                // Check for the Description attribute. 
                if (attr.GetType() == typeof(DescriptionAttribute))
                    Console.WriteLine("Module {0} has the description " +
                        "\"{1}\".", module.Name, 
                        ((DescriptionAttribute)attr).Description);
                // Check for the CLSCompliant attribute. 
                else if (attr.GetType() == typeof(CLSCompliantAttribute))
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.Name,
                        ((CLSCompliantAttribute)attr).IsCompliant ? 
                            "is" : "is not");
            }
        }
    }
}

/*
 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".
 */

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft