Freigeben über


Module.GetCustomAttributes Method (Type, Boolean)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns custom attributes of the specified type.

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

Syntax

'Declaration
Public Overridable Function GetCustomAttributes ( _
    attributeType As Type, _
    inherit As Boolean _
) As Object()
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: array<System.Object[]
An array that contains all custom attributes of the specified type.

Implements

ICustomAttributeProvider.GetCustomAttributes(Type, Boolean)

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentException

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

Examples

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.

Imports System.Reflection

' Define a module-level attribute.
<Module: MySimpleAttribute("module-level")> 

Class Example
    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
        Dim moduleArray() As [Module] = _
            [Assembly].GetExecutingAssembly().GetModules()
        Dim myModule As [Module] = moduleArray(0)
        Dim attributes() As Object = _
            myModule.GetCustomAttributes(GetType(MySimpleAttribute), True)
        For Each o As Object In attributes
            outputBlock.Text &= _
                String.Format("Found this attribute on myModule: {0}" & vbLf, o.ToString())
        Next o
    End Sub 
End Class 

'A very simple custom attribute.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Module)> _
Public Class MySimpleAttribute
    Inherits Attribute
    Private name As String
    Public Sub New(ByVal newName As String)
        name = newName
    End Sub 
End Class 

' This example produces output similar to the following:
'
'Found this attribute on myModule: SilverlightApplication.MySimpleAttribute.
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.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.