CustomAttributeTypedArgumentとは? わかりやすく解説

CustomAttributeTypedArgument プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ ArgumentType 型付き引数の型を取得します
パブリック プロパティ Value 型付き引数の値を取得します
参照参照

関連項目

CustomAttributeTypedArgument 構造体
System.Reflection 名前空間
CustomAttributeData クラス
CustomAttributeNamedArgument 構造体
ReflectionOnlyLoad

CustomAttributeTypedArgument メソッド


パブリック メソッドパブリック メソッド

参照参照

関連項目

CustomAttributeTypedArgument 構造体
System.Reflection 名前空間
CustomAttributeData クラス
CustomAttributeNamedArgument 構造体
ReflectionOnlyLoad

CustomAttributeTypedArgument メンバ


CustomAttributeTypedArgument 構造体

メモ : この構造体は、.NET Framework version 2.0新しく追加されたものです。

検査コンテキストにおけるカスタム属性型付き引数表します

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

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Structure CustomAttributeTypedArgument
Dim instance As CustomAttributeTypedArgument
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public struct CustomAttributeTypedArgument
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public value class CustomAttributeTypedArgument
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class CustomAttributeTypedArgument
 extends ValueType
JScript では、構造体使用できますが、新規に宣言することはできません。
解説解説

リフレクションのみのコンテキスト検査されているコード実行できないため、カスタム属性インスタンス作成し、System.Attribute.GetCustomAttributes や System.Reflection.MemberInfo.GetCustomAttributes などのメソッド使用してカスタム属性プロパティ調べることによってカスタム属性検査できるとは限りません。また、属性型コード自体リフレクションのみのコンテキスト読み込んで実行することはできません。

CustomAttributeNamedArgument 構造体は、属性コンストラクタ実行せずに、カスタム属性インスタンスに対して指定されている位置指定引数の型と値にアクセスできるようにするために CustomAttributeData クラス使用しますまた、この構造体は、カスタム属性型の対応するプロパティコード実行せずに名前付引数の型と値にアクセスできるようにします。

属性インスタンスすべての位置指定引数および名前付引数の型と値は、CustomAttributeTypedArgument 構造体によって提供されます。CustomAttributeData.ConstructorArguments プロパティ返す位置属性CustomAttributeTypedArgument 構造体直接表されますが、CustomAttributeData.NamedArguments プロパティ返す前付引数CustomAttributeNamedArgument 構造体表されます。前付引数CustomAttributeTypedArgument 構造体取得するには、CustomAttributeNamedArgument.TypedValue プロパティ使用します

CustomAttributeData クラスインスタンス作成するには、staticGetCustomAttributes ファクトリ メソッド使用します

使用例使用例

2 つコンストラクタ1 つプロパティ使用してカスタム属性定義するコード例次に示します属性は、アセンブリアセンブリ宣言された型、型のメソッド、およびメソッドパラメータに対して適用されます。アセンブリ実行した場合アセンブリ自身リフレクションのみのコンテキスト読み込まれカスタム属性表示されます。

Imports System
Imports System.Reflection
Imports System.Collections.Generic

' The example attribute is applied to the assembly.
<Assembly:Example(ExampleKind.ThirdKind, Note:="This is a note
 on the assembly.")>

' An enumeration used by the ExampleAttribute class.
Public Enum ExampleKind
    FirstKind
    SecondKind
    ThirdKind
    FourthKind
End Enum

' An example attribute. The attribute can be applied to all
' targets, from assemblies to parameters.
'
<AttributeUsage(AttributeTargets.All)> _
Public Class ExampleAttribute
    Inherits Attribute

    ' Data for properties.
    Private kindValue As ExampleKind
    Private noteValue As String

    ' Constructors. The parameterless constructor (.ctor) calls
    ' the constructor that specifies ExampleKind, and supplies the
    ' default value.
    '
    Public Sub New(ByVal
 initKind As ExampleKind)
        kindValue = initKind
    End Sub
    Public Sub New()
        Me.New(ExampleKind.FirstKind)
    End Sub

    ' Properties. The Note property must be read/write, so that it can
    ' be used as a named parameter.
    '
    Public ReadOnly Property
 Kind As ExampleKind
        Get
            Return kindValue 
        End Get
    End Property
    Public Property Note As
 String
        Get
            Return noteValue 
        End Get
        Set
            noteValue = value
        End Set
    End Property
End Class

' The example attribute is applied to the test class.
'
<Example(ExampleKind.SecondKind, Note:="This is a note on the
 class.")> _
Public Class Test
    ' The example attribute is applied to a method, using the
    ' parameterless constructor and supplying a named argument.
    ' The attribute is also applied to the method parameter.
    '
    <Example(Note:="This is a note on a method.")>
 _
    Public Sub TestMethod(<Example()>
 ByVal arg As Object)
    End Sub

    ' Sub Main gets objects representing the assembly, the test
    ' type, the test method, and the method parameter. Custom
    ' attribute data is displayed for each of these.
    '
    Public Shared Sub Main()
        Dim asm As [Assembly] = Assembly.ReflectionOnlyLoad("Source")
        Dim t As Type = asm.GetType("Test")
        Dim m As MethodInfo = t.GetMethod("TestMethod")
        Dim p() As ParameterInfo = m.GetParameters()

        Console.WriteLine(vbCrLf & "Attributes for assembly:
 {0}", asm)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm))
        Console.WriteLine(vbCrLf & "Attributes for type: {0}",
 t)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(t))
        Console.WriteLine(vbCrLf & "Attributes for member:
 {0}", m)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(m))
        Console.WriteLine(vbCrLf & "Attributes for parameter:
 {0}", p)
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(p(0)))
    End Sub

    Private Shared Sub ShowAttributeData(
 _
        ByVal attributes As IList(Of
 CustomAttributeData))

        For Each cad As
 CustomAttributeData _
            In CType(attributes, IEnumerable(Of
 CustomAttributeData))

            Console.WriteLine("   {0}", cad)
            Console.WriteLine("      Constructor: {0}",
 cad.Constructor)

            Console.WriteLine("      Constructor arguments:")
            For Each cata As
 CustomAttributeTypedArgument _
                In CType(cad.ConstructorArguments, IEnumerable(Of
 CustomAttributeTypedArgument))

                Console.WriteLine("         Type: {0} Value: {1}",
 _
                    cata.ArgumentType, cata.Value)
            Next

            Console.WriteLine("      Named arguments:")
            For Each cana As
 CustomAttributeNamedArgument _
                In CType(cad.NamedArguments, IEnumerable(Of
 CustomAttributeNamedArgument))

                Dim cata As CustomAttributeTypedArgument
 = _
                    cana.TypedValue
                Console.WriteLine("         MemberInfo: {0}",
 _
                    cana.MemberInfo)
                Console.WriteLine("         Type: {0} Value: {1}",
 _
                    cata.ArgumentType, cata.Value)
            Next
        Next
    End Sub
End Class

' This code example produces output similar to the following:
'
'Attributes for assembly: source, Version=0.0.0.0, Culture=neutral,
 PublicKeyToken=null
'   [ExampleAttribute((ExampleKind)2, Note = "This is a note on
 the assembly.")]
'      Constructor: Void .ctor(ExampleKind)
'      Constructor arguments:
'         Type: ExampleKind Value: 2
'      Named arguments:
'         MemberInfo: System.String Note
'         Type: System.String Value: This is a note on the assembly.
'   [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
'      Constructor: Void .ctor(Int32)
'      Constructor arguments:
'         Type: System.Int32 Value: 8
'      Named arguments:
'
'Attributes for type: Test
'   [ExampleAttribute((ExampleKind)1, Note = "This is a note on
 the class.")]
'      Constructor: Void .ctor(ExampleKind)
'      Constructor arguments:
'         Type: ExampleKind Value: 1
'      Named arguments:
'         MemberInfo: System.String Note
'         Type: System.String Value: This is a note on the class.
'
'Attributes for member: Void TestMethod(System.Object)
'   [ExampleAttribute(Note = "This is a note on a method.")]
'      Constructor: Void .ctor()
'      Constructor arguments:
'      Named arguments:
'         MemberInfo: System.String Note
'         Type: System.String Value: This is a note on a method.
'
'Attributes for parameter: System.Object arg
'   [ExampleAttribute()]
'      Constructor: Void .ctor()
'      Constructor arguments:
'      Named arguments:
using System;
using System.Reflection;
using System.Collections.Generic;

// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind.ThirdKind, Note="This is a note on the assembly.")]

// An enumeration used by the ExampleAttribute class.
public enum ExampleKind
{
    FirstKind, 
    SecondKind, 
    ThirdKind, 
    FourthKind
};

// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : Attribute
{
    // Data for properties.
    private ExampleKind kindValue;
    private string noteValue;

    // Constructors. The parameterless constructor (.ctor) calls
    // the constructor that specifies ExampleKind, and supplies the
    // default value.
    //
    public ExampleAttribute(ExampleKind initKind)
    {
        kindValue = initKind;
    }
    public ExampleAttribute() : this(ExampleKind.FirstKind)
 {}

    // Properties. The Note property must be read/write, so that it
    // can be used as a named parameter.
    //
    public ExampleKind Kind { get { return
 kindValue; }}
    public string Note    
    {
        get { return noteValue; }
        set { noteValue = value; }
    }
}

// The example attribute is applied to the test class.
//
[Example(ExampleKind.SecondKind, Note="This is a note on the class.")]
public class Test
{
    // The example attribute is applied to a method, using the
    // parameterless constructor and supplying a named argument.
    // The attribute is also applied to the method parameter.
    //
    [Example(Note="This is a note on a method.")]
    public void TestMethod([Example] object
 arg) { }

    // Main() gets objects representing the assembly, the test
    // type, the test method, and the method parameter. Custom
    // attribute data is displayed for each of these.
    //
    public static void Main()
    {
        Assembly asm = Assembly.ReflectionOnlyLoad("Source");
        Type t = asm.GetType("Test");
        MethodInfo m = t.GetMethod("TestMethod");
        ParameterInfo[] p = m.GetParameters();

        Console.WriteLine("\r\nAttributes for assembly: {0}",
 asm);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm));
        Console.WriteLine("\r\nAttributes for type: {0}",
 t);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(t));
        Console.WriteLine("\r\nAttributes for member: {0}",
 m);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(m));
        Console.WriteLine("\r\nAttributes for parameter:
 {0}", p);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(p[0]));
    }

    private static void
 ShowAttributeData(
        IList<CustomAttributeData> attributes)
    {
        foreach( CustomAttributeData cad in
 attributes )
        {
            Console.WriteLine("   {0}", cad);
            Console.WriteLine("      Constructor: {0}", cad.Constructor);

            Console.WriteLine("      Constructor arguments:");
            foreach( CustomAttributeTypedArgument cata 
                in cad.ConstructorArguments )
            {
                Console.WriteLine("         Type: {0} Value: {1}", 
                    cata.ArgumentType, cata.Value);
            }

            Console.WriteLine("      Named arguments:");
            foreach( CustomAttributeNamedArgument cana 
                in cad.NamedArguments )
            {
                CustomAttributeTypedArgument cata = cana.TypedValue;
                Console.WriteLine("         MemberInfo: {0}", 
                    cana.MemberInfo);
                Console.WriteLine("         Type: {0} Value: {1}", 
                    cata.ArgumentType, cata.Value);
            }
        }
    }
}

/* This code example produces output similar to the following:

Attributes for assembly: source, Version=0.0.0.0, Culture=neutral,
 PublicKeyToken=null
   [ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
      Constructor: Void .ctor(ExampleKind)
      Constructor arguments:
         Type: ExampleKind Value: 2
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on the assembly.
   [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
      Constructor: Void .ctor(Int32)
      Constructor arguments:
         Type: System.Int32 Value: 8
      Named arguments:

Attributes for type: Test
   [ExampleAttribute((ExampleKind)1, Note = "This is a note on the class.")]
      Constructor: Void .ctor(ExampleKind)
      Constructor arguments:
         Type: ExampleKind Value: 1
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on the class.

Attributes for member: Void TestMethod(System.Object)
   [ExampleAttribute(Note = "This is a note on a method.")]
      Constructor: Void .ctor()
      Constructor arguments:
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on a method.

Attributes for parameter: System.Object arg
   [ExampleAttribute()]
      Constructor: Void .ctor()
      Constructor arguments:
      Named arguments:
*/
using namespace System;
using namespace System::Reflection;
using namespace System::Collections::Generic;

// An enumeration used by the ExampleAttribute class.
public enum class ExampleKind
{
   FirstKind, SecondKind, ThirdKind, FourthKind
};

// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets::All)]
public ref class ExampleAttribute: public
 Attribute
{
private:
   // Data for properties.
   ExampleKind kindValue;
   String^ noteValue;

   // Constructors. The parameterless constructor (.ctor) calls
   // the constructor that specifies ExampleKind, and supplies the
   // default value.
   //
   void ExampleAttributeInitialize( ExampleKind initKind )
   {
      kindValue = initKind;
   }
public:
   ExampleAttribute()
   {
      ExampleAttributeInitialize( ExampleKind::FirstKind );
   }
   ExampleAttribute( ExampleKind initKind )
   {
      ExampleAttributeInitialize( initKind );
   }

   // Properties. The Note property must be read/write, so that it
   // can be used as a named parameter.
   //
   property ExampleKind Kind 
   {
      ExampleKind get()
      {
         return kindValue;
      }
   }
   property String^ Note 
   {
      String^ get()
      {
         return noteValue;
      }

      void set( String^ value )
      {
         noteValue = value;
      }
   }
};

// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind::ThirdKind,Note="This is a note on the assembly.")];

// The example attribute is applied to the test class.
//
[Example(ExampleKind::SecondKind,Note="This is a note on the class.")]
public ref class Test
{
public:
   // The example attribute is applied to a method, using the
   // parameterless constructor and supplying a named argument.
   // The attribute is also applied to the method parameter.
   //
   [Example(Note="This is a note on a method.")]
   void TestMethod( [Example] Object^ arg ){}

   // Main() gets objects representing the assembly, the test
   // type, the test method, and the method parameter. Custom
   // attribute data is displayed for each of these.
   //
   static void Main()
   {
      Assembly^ assembly = Assembly::ReflectionOnlyLoad( "Source" );
      Type^ t = assembly->GetType( "Test" );
      MethodInfo^ m = t->GetMethod( "TestMethod" );
      array<ParameterInfo^>^p = m->GetParameters();

      Console::WriteLine( "\r\nAttributes for assembly: {0}",
 assembly );
      ShowAttributeData( CustomAttributeData::GetCustomAttributes( assembly ) );
      Console::WriteLine( "\r\nAttributes for type: {0}",
 t );
      ShowAttributeData( CustomAttributeData::GetCustomAttributes( t ) );
      Console::WriteLine( "\r\nAttributes for member: {0}",
 m );
      ShowAttributeData( CustomAttributeData::GetCustomAttributes( m ) );
      Console::WriteLine( "\r\nAttributes for parameter:
 {0}", p );
      ShowAttributeData( CustomAttributeData::GetCustomAttributes( p[ 0 ] ) );
   }

private:
   static void ShowAttributeData( IList<
 CustomAttributeData^ >^ attributes )
   {
      for each ( CustomAttributeData^ cad in
 attributes )
      {
         Console::WriteLine( "   {0}", cad );
         Console::WriteLine( "      Constructor: {0}", cad->Constructor
 );

         Console::WriteLine( "      Constructor arguments:" );
         for each ( CustomAttributeTypedArgument^ cata in
 cad->ConstructorArguments )
         {
            Console::WriteLine( "         Type: {0} Value: {1}",
               cata->ArgumentType, cata->Value );
         }

         Console::WriteLine( "      Named arguments:" );
         for each ( CustomAttributeNamedArgument cana in
 cad->NamedArguments )
         {
            CustomAttributeTypedArgument cata = cana.TypedValue;
            Console::WriteLine( "         MemberInfo: {0}", cana.MemberInfo
 );
            Console::WriteLine( "         Type: {0} Value: {1}", cata.ArgumentType,
 cata.Value );
         }
      }
   }
};

int main()
{
   Test::Main();
}

/* This code example produces output similar to the following:

Attributes for assembly: source, Version=0.0.0.0, Culture=neutral,
 PublicKeyToken=null
   [ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
      Constructor: Void .ctor(ExampleKind)
      Constructor arguments:
         Type: ExampleKind Value: 2
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on the assembly.
   [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
      Constructor: Void .ctor(Int32)
      Constructor arguments:
         Type: System.Int32 Value: 8
      Named arguments:

Attributes for type: Test
   [ExampleAttribute((ExampleKind)1, Note = "This is a note on the class.")]
      Constructor: Void .ctor(ExampleKind)
      Constructor arguments:
         Type: ExampleKind Value: 1
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on the class.

Attributes for member: Void TestMethod(System.Object)
   [ExampleAttribute(Note = "This is a note on a method.")]
      Constructor: Void .ctor()
      Constructor arguments:
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on a method.

Attributes for parameter: System.Object arg
   [ExampleAttribute()]
      Constructor: Void .ctor()
      Constructor arguments:
      Named arguments:
*/
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
CustomAttributeTypedArgument メンバ
System.Reflection 名前空間
CustomAttributeData クラス
CustomAttributeNamedArgument 構造体
ReflectionOnlyLoad


このページでは「.NET Framework クラス ライブラリ リファレンス」からCustomAttributeTypedArgumentを検索した結果を表示しています。
Weblioに収録されているすべての辞書からCustomAttributeTypedArgumentを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からCustomAttributeTypedArgument を検索

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

辞書ショートカット

すべての辞書の索引

「CustomAttributeTypedArgument」の関連用語

CustomAttributeTypedArgumentのお隣キーワード
検索ランキング

   

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



CustomAttributeTypedArgumentのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS