This documentation is archived and is not being maintained.

Module.GetCustomAttributes Method (Boolean)

.NET Framework 1.1

Returns all custom attributes.

[Visual Basic]
Overloads Public Overridable Function GetCustomAttributes( _
   ByVal inherit As Boolean _
) As Object() Implements ICustomAttributeProvider.GetCustomAttributes
[C#]
public virtual object[] GetCustomAttributes(
 bool inherit
);
[C++]
public: virtual Object* GetCustomAttributes(
 bool inherit
)  __gc[];
[JScript]
public function GetCustomAttributes(
   inherit : Boolean
) : Object[];

Parameters

inherit
This argument is ignored for objects of this type.

Return Value

An array of type Object containing all custom attributes.

Implements

ICustomAttributeProvider.GetCustomAttributes

Example

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

[Visual Basic] 
Imports System
Imports System.Reflection
' Define a module-level attribute.
<Module: ReflectionModule_Examples.MySimpleAttribute("module-level")> 
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
            attributes = myModule.GetCustomAttributes(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
    '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;
            attributes = myModule.GetCustomAttributes(true);
            foreach(Object o in attributes)
            {
                Console.WriteLine("Found this attribute on myModule: {0}.", 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;
        }
    }
}

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

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

namespace ReflectionModule_Examples
{
   //Define a module-level attribute.
   //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;
      }
   };

   [module: MySimpleAttribute(S"module-level")];

   __gc class MyMainClass
   {
   };
}

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[];
   attributes = myModule->GetCustomAttributes(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: