Module.GetCustomAttributes Method
.NET Framework 1.1
Returns custom attributes.
Overload List
Returns all custom attributes.
[Visual Basic] Overloads Public Overridable Function GetCustomAttributes(Boolean) As Object() Implements ICustomAttributeProvider.GetCustomAttributes
[C#] public virtual object[] GetCustomAttributes(bool);
[C++] public: virtual Object* GetCustomAttributes(bool) __gc[];
[JScript] public function GetCustomAttributes(Boolean) : Object[];
Gets custom attributes of the specified type.
[Visual Basic] Overloads Public Overridable Function GetCustomAttributes(Type, Boolean) As Object() Implements ICustomAttributeProvider.GetCustomAttributes
[C#] public virtual object[] GetCustomAttributes(Type, bool);
[C++] public: virtual Object* GetCustomAttributes(Type*, bool) __gc[];
[JScript] public function GetCustomAttributes(Type, Boolean) : Object[];
Example
[Visual Basic, C#, C++] The following example displays the module names of the specified type that match the specified search criteria.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of GetCustomAttributes. For other examples that might be available, see the individual overload topics.
[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
in the upper-left corner of the page.