Module.GetCustomAttributes メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Module.GetCustomAttributes メソッドの意味・解説 

Module.GetCustomAttributes メソッド (Type, Boolean)

指定された型のカスタム属性取得します

名前空間: System.Reflection
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Function
 GetCustomAttributes ( _
    attributeType As Type, _
    inherit As Boolean _
) As Object()
Dim instance As Module
Dim attributeType As Type
Dim inherit As Boolean
Dim returnValue As Object()

returnValue = instance.GetCustomAttributes(attributeType, inherit)
public virtual Object[] GetCustomAttributes (
    Type attributeType,
    bool inherit
)
public:
virtual array<Object^>^ GetCustomAttributes (
    Type^ attributeType, 
    bool inherit
)
public Object[] GetCustomAttributes (
    Type attributeType, 
    boolean inherit
)
public function GetCustomAttributes (
    attributeType : Type, 
    inherit : boolean
) : Object[]

パラメータ

attributeType

取得する属性の型。

inherit

この型のオブジェクトでは、この引数無視されます。

戻り値
指定された型のすべてのカスタム属性格納しているオブジェクト型配列

例外例外
例外種類条件

ArgumentNullException

attributeTypenull 参照 (Visual Basic では Nothing) です。

使用例使用例

指定した型で、指定した検索条件一致するモジュールの名前を表示する例を次に示します

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
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;
        }
    }
}
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 = 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 ];
   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 );
   }
}

package ReflectionModule_Examples;

import System.*;
import System.Reflection.*;

//Define a module-level attribute.
/** @module ReflectionModule_Examples.MySimpleAttribute("module-level")
 */
class MyMainClass
{
    public static void main(String[]
 args)
    {
        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 = (Module)moduleArray.get_Item(0);
        Object attributes[];
        //Get only MySimpleAttribute attributes for this module.
        attributes = myModule.GetCustomAttributes(
            myModule.GetType("ReflectionModule_Examples.MySimpleAttribute",
 
            false, false), true);
        for (int iCtr = 0; iCtr < attributes.length;
 iCtr++) {
            Object o = attributes[iCtr];
            Console.WriteLine("Found this attribute on myModule:
 {0}", 
                o.ToString());
        }
    } //main
} //MyMainClass

// Define a very simple custom attribute
/** @attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)
 */
public class MySimpleAttribute extends Attribute
{
    private String name;

    public MySimpleAttribute(String newName)
    {
        name = newName;
    } //MySimpleAttribute
} //MySimpleAttribute
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Module.GetCustomAttributes メソッド


Module.GetCustomAttributes メソッド (Boolean)

すべてのカスタム属性返します

名前空間: System.Reflection
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Function
 GetCustomAttributes ( _
    inherit As Boolean _
) As Object()
public virtual Object[] GetCustomAttributes (
    bool inherit
)
public:
virtual array<Object^>^ GetCustomAttributes (
    bool inherit
)
public Object[] GetCustomAttributes (
    boolean inherit
)
public function GetCustomAttributes (
    inherit : boolean
) : Object[]

パラメータ

inherit

この型のオブジェクトでは、この引数無視されます。

戻り値
すべてのカスタム属性格納されているオブジェクト型配列

使用例使用例

指定した検索条件一致するモジュール名を表示する例を次に示します

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
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;
        }
    }
}
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 = 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 ];
   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 );
   }
}

package ReflectionModule_Examples; 

import System.*;
import System.Reflection.*;

//Define a module-level attribute.
/** @module ReflectionModule_Examples.MySimpleAttribute("module-level")
 */
class MyMainClass
{
    public static void main(String[]
 args)
    {
        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 = (Module)moduleArray.get_Item(0);
        Object attributes[];
        attributes = myModule.GetCustomAttributes(true);
        for (int iCtr = 0; iCtr < attributes.length;
 iCtr++) {
            Object o = ( Object)attributes[iCtr];
            Console.WriteLine("Found this attribute on myModule:
 {0}.", 
                o.ToString());
        }
    } //main
} //MyMainClass

//A very simple custom attribute.
/** @attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)
 */
public class MySimpleAttribute extends Attribute
{
    private String name;

    public MySimpleAttribute(String newName)
    {
        name = newName;
    } //MySimpleAttribute
} //MySimpleAttribute
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Module.GetCustomAttributes メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Module.GetCustomAttributes メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS