Export (0) Print
Expand All

Module.GetCustomAttributes Method (Type, Boolean)

Gets custom attributes of the specified type.

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

public virtual Object[] GetCustomAttributes (
	Type attributeType,
	bool inherit
)
public Object[] GetCustomAttributes (
	Type attributeType, 
	boolean inherit
)
public function GetCustomAttributes (
	attributeType : Type, 
	inherit : boolean
) : Object[]

Parameters

attributeType

The type of attribute to get.

inherit

This argument is ignored for objects of this type.

Return Value

An array of type Object containing all custom attributes of the specified type.

Exception typeCondition

ArgumentNullException

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

The following example displays the module names of the specified type that match the specified search criteria.

using System;
using System.Reflection;
//Define a module-level attribute.
[module: ReflectionModule_Examples.MySimpleAttribute("module-level")]
namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
            // In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];
            object[] attributes;
            //Get only MySimpleAttribute attributes for this module.
            attributes = myModule.GetCustomAttributes(
                myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", false, false),
                true);
            foreach(Object o in attributes)
            {
                Console.WriteLine("Found this attribute on myModule: {0}", o.ToString());
            }
        }
    }

    // Define a very simple custom attribute
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)]
    public class MySimpleAttribute : Attribute
    {
        private string name;

        public MySimpleAttribute(string newName)
        {
            name = newName;
        }
    }
}

package ReflectionModule_Examples;

import System.*;
import System.Reflection.*;

//Define a module-level attribute.
/** @module ReflectionModule_Examples.MySimpleAttribute("module-level")
 */
class MyMainClass
{
    public static void main(String[] args)
    {
        Module moduleArray[];
        moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
        // In a simple project with only one module, the module at index
        // 0 will be the module containing these classes.
        Module myModule = (Module)moduleArray.get_Item(0);
        Object attributes[];
        //Get only MySimpleAttribute attributes for this module.
        attributes = myModule.GetCustomAttributes(
            myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", 
            false, false), true);
        for (int iCtr = 0; iCtr < attributes.length; iCtr++) {
            Object o = attributes[iCtr];
            Console.WriteLine("Found this attribute on myModule: {0}", 
                o.ToString());
        }
    } //main
} //MyMainClass

// Define a very simple custom attribute
/** @attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)
 */
public class MySimpleAttribute extends Attribute
{
    private String name;

    public MySimpleAttribute(String newName)
    {
        name = newName;
    } //MySimpleAttribute
} //MySimpleAttribute

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

Community Additions

ADD
Show:
© 2014 Microsoft