Export (0) Print
Expand All

Attribute.GetCustomAttributes Method (Module, Type, Boolean)

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

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

public static Attribute[] GetCustomAttributes (
	Module element,
	Type attributeType,
	bool inherit
)
public static Attribute[] GetCustomAttributes (
	Module element, 
	Type attributeType, 
	boolean inherit
)
public static function GetCustomAttributes (
	element : Module, 
	attributeType : Type, 
	inherit : boolean
) : Attribute[]
Not applicable.

Parameters

element

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

attributeType

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

inherit

This parameter is ignored, and does not affect the operation of this method.

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 return value contains the custom attributes for ancestors of element if inherit is true.

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".
 */

package CustAttrs2JSL; 
import System.*;
import System.Reflection.*;
import 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)
 */
class DemoClass
{
    public static void main(String[] args)
    {
        Type clsType = DemoClass.class.ToType();

        // Get the Module type to access its metadata.
        Module module = clsType.get_Module();

        // Iterate through all the attributes for the module.
        for (int iCtr = 0; iCtr < Attribute.GetCustomAttributes(module).
            get_Length(); iCtr++) {
            Attribute attr = (Attribute)Attribute.GetCustomAttributes(module).
                get_Item(iCtr);
            // Check for the Description attribute.
            if (attr.GetType().Equals(DescriptionAttribute.class.ToType())) {
                Console.WriteLine("Module {0} has the description " + 
                    "\"{1}\".", module.get_Name(),
                    ((DescriptionAttribute)attr).get_Description());
            }
            // Check for the CLSCompliant attribute.
            else {
                if (attr.GetType().Equals(CLSCompliantAttribute.class.
                    ToType())) {
                    Console.WriteLine("Module {0} {1} CLSCompliant.",
                        module.get_Name(), (((CLSCompliantAttribute)attr).
                        get_IsCompliant()) ? "is" : "is not");
                }
            }
        }
    } //main
} //DemoClass   

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

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft