Module.GetCustomAttributes Yöntem

Tanım

Özel öznitelikleri döndürür.

Aşırı Yüklemeler

GetCustomAttributes(Boolean)

Tüm özel öznitelikleri döndürür.

GetCustomAttributes(Type, Boolean)

Belirtilen türdeki özel öznitelikleri alır.

GetCustomAttributes(Boolean)

Kaynak:
Module.cs
Kaynak:
Module.cs
Kaynak:
Module.cs

Tüm özel öznitelikleri döndürür.

public:
 virtual cli::array <System::Object ^> ^ GetCustomAttributes(bool inherit);
public virtual object[] GetCustomAttributes (bool inherit);
abstract member GetCustomAttributes : bool -> obj[]
override this.GetCustomAttributes : bool -> obj[]
Public Overridable Function GetCustomAttributes (inherit As Boolean) As Object()

Parametreler

inherit
Boolean

Bu türdeki nesneler için bu bağımsız değişken yoksayılır.

Döndürülenler

Object[]

Tüm özel öznitelikleri içeren tür Object dizisi.

Uygulamalar

Örnekler

Aşağıdaki örnek, belirtilen arama ölçütlerine uyan modül adlarını görüntüler.

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 ref class MySimpleAttribute: public Attribute
   {
   private:
      String^ name;

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

   };


   [module:MySimpleAttribute("module-level")];
   ref class MyMainClass{};

}

int main()
{
   array<System::Reflection::Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MySimpleAttribute::typeid->Assembly->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 ];
   array<Object^>^attributes;
   attributes = myModule->GetCustomAttributes( true );
   IEnumerator^ myEnum = attributes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "Found this attribute on myModule: {0}.", o );
   }
}
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 = typeof(MyMainClass).Assembly.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;
        }
    }
}
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 = GetType(MyMainClass).Assembly.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
    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
End Namespace 'ReflectionModule_Examples

Şunlara uygulanır

GetCustomAttributes(Type, Boolean)

Kaynak:
Module.cs
Kaynak:
Module.cs
Kaynak:
Module.cs

Belirtilen türdeki özel öznitelikleri alır.

public:
 virtual cli::array <System::Object ^> ^ GetCustomAttributes(Type ^ attributeType, bool inherit);
public virtual object[] GetCustomAttributes (Type attributeType, bool inherit);
abstract member GetCustomAttributes : Type * bool -> obj[]
override this.GetCustomAttributes : Type * bool -> obj[]
Public Overridable Function GetCustomAttributes (attributeType As Type, inherit As Boolean) As Object()

Parametreler

attributeType
Type

Alınacak öznitelik türü.

inherit
Boolean

Bu türdeki nesneler için bu bağımsız değişken yoksayılır.

Döndürülenler

Object[]

Belirtilen türdeki Object tüm özel öznitelikleri içeren tür dizisi.

Uygulamalar

Özel durumlar

attributeType, null değeridir.

attributeType çalışma zamanı tarafından sağlanan bir Type nesne değildir. Örneğin, attributeType bir TypeBuilder nesnedir.

Örnekler

Aşağıdaki örnek, belirtilen arama ölçütlerine uyan belirtilen türün modül adlarını görüntüler.

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 ref class MySimpleAttribute: public Attribute
   {
   private:
      String^ name;

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

   };

}


//Define a module-level attribute.

[module:ReflectionModule_Examples::MySimpleAttribute("module-level")];
int main()
{
   array<System::Reflection::Module^>^moduleArray;
   moduleArray = ReflectionModule_Examples::MySimpleAttribute::typeid->Assembly->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 ];
   array<Object^>^attributes;
   
   //Get only MySimpleAttribute attributes for this module.
   attributes = myModule->GetCustomAttributes( myModule->GetType( "ReflectionModule_Examples.MySimpleAttribute", false, false ), true );
   IEnumerator^ myEnum = attributes->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "Found this attribute on myModule: {0}", o );
   }
}
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 = typeof(MyMainClass).Assembly.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;
        }
    }
}
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 = GetType(MyMainClass).Assembly.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
    End Class
    ' 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
    End Class
End Namespace 'ReflectionModule_Examples

Şunlara uygulanır