Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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
)

Parameters

attributeType
Type: System.Type

The type of attribute to get.

inherit
Type: System.Boolean

This argument is ignored for objects of this type.

Return Value

Type: System.Object[]
An array of type Object containing all custom attributes of the specified type.

Implements

ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)
ExceptionCondition
ArgumentNullException

attributeType is null.

ArgumentException

attributeType is not a Type object supplied by the runtime. For example, attributeType is a TypeBuilder object.

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;
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.