Export (0) Print
Expand All

Module.GetCustomAttributes Method (Type, Boolean)

.NET Framework 1.1

Gets custom attributes of the specified type.

[Visual Basic]
Overloads Public Overridable Function GetCustomAttributes( _
   ByVal attributeType As Type, _
   ByVal inherit As Boolean _
) As Object() Implements ICustomAttributeProvider.GetCustomAttributes
[C#]
public virtual object[] GetCustomAttributes(
 Type attributeType,
 bool inherit
);
[C++]
public: virtual Object* GetCustomAttributes(
 Type* attributeType,
 bool inherit
)  __gc[];
[JScript]
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.

Implements

ICustomAttributeProvider.GetCustomAttributes

Exceptions

Exception Type Condition
ArgumentNullException attributeType is a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The following example displays the module names of the specified type that match the specified search criteria.

[Visual Basic] 
Imports System
Imports System.Reflection
' Define a module-level attribute.
<Module: ReflectionModule_Examples.MySimpleAttribute("module-level")> 
' This code assumes that the root namespace is set to empty("").
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]
            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.
            Dim myModule As [Module] = moduleArray(0)
            Dim attributes() As Object
            ' Get only MySimpleAttribute attributes for this module.
            attributes = myModule.GetCustomAttributes( _
                myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", _
                False, False), True)
            Dim o As [Object]
            For Each o In attributes
                Console.WriteLine("Found this attribute on myModule: {0}", o.ToString())
            Next o
        End Sub 'Main
    End Class 'MyMainClass
    ' Define 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 'New
    End Class 'MySimpleAttribute
End Namespace 'ReflectionModule_Examples

[C#] 
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;
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Collections;

namespace ReflectionModule_Examples
{
   // Define a very simple custom attribute
   [AttributeUsage(AttributeTargets::Class | AttributeTargets::Module)]
   public __gc class MySimpleAttribute : public Attribute
   {
   private:
      String* name;

   public:
      MySimpleAttribute(String* newName)
      {
         name = newName;
      }
   };
}

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

int main()
{
   System::Reflection::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.
   System::Reflection::Module* myModule = moduleArray[0];
   Object* attributes[];
   //Get only MySimpleAttribute attributes for this module.
   attributes = myModule->GetCustomAttributes(myModule->GetType(S"ReflectionModule_Examples.MySimpleAttribute", false, false),
      true);
   IEnumerator* myEnum = attributes->GetEnumerator();
   while (myEnum->MoveNext()) {
      Object* o = __try_cast<Object*>(myEnum->Current);
      Console::WriteLine(S"Found this attribute on myModule: {0}", o);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Module Class | Module Members | System.Reflection Namespace | Module.GetCustomAttributes Overload List

Show:
© 2014 Microsoft