GetCustomAttributes Method (Type, Boolean)
Collapse the table of content
Expand the table of content

Module.GetCustomAttributes Method (Type, Boolean)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns custom attributes of the specified type.

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

public virtual Object[] GetCustomAttributes(
	Type attributeType,
	bool inherit


Type: System.Type
The type of attribute to get.
Type: System.Boolean
This argument is ignored for objects of this type.

Return Value

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


ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)


attributeType is null.


attributeType is not a Type object supplied by the runtime.

The following example defines an attribute and applies it to the example's module. When the example runs, it retrieves the attributes that were applied to the module and displays them.

using System;
using System.Reflection;

//Define a module-level attribute.
[module: MySimpleAttribute("module-level")]

class Example
    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
        Module[] moduleArray = Assembly.GetExecutingAssembly().GetModules();
        Module myModule = moduleArray[0];
        object[] attributes = 
            myModule.GetCustomAttributes(typeof(MySimpleAttribute), true);
        foreach (Object o in attributes)
            outputBlock.Text += 
               String.Format("Found this attribute on myModule: {0}.\n", o.ToString());

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

    public MySimpleAttribute(string newName)
        name = newName;

/* This example produces output similar to the following:

Found this attribute on myModule: MySimpleAttribute.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft