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

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

AssemblyBuilder.SetCustomAttribute メソッド (CustomAttributeBuilder)

カスタム属性ビルダ使用して、このアセンブリカスタム属性設定します

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

Public Sub SetCustomAttribute ( _
    customBuilder As CustomAttributeBuilder _
)
Dim instance As AssemblyBuilder
Dim customBuilder As CustomAttributeBuilder

instance.SetCustomAttribute(customBuilder)
public void SetCustomAttribute (
    CustomAttributeBuilder customBuilder
)
public:
void SetCustomAttribute (
    CustomAttributeBuilder^ customBuilder
)
public void SetCustomAttribute (
    CustomAttributeBuilder customBuilder
)
public function SetCustomAttribute (
    customBuilder : CustomAttributeBuilder
)

パラメータ

customBuilder

カスタム属性定義するためのヘルパー クラスインスタンス

例外例外
例外種類条件

ArgumentNullException

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

SecurityException

呼び出し元に必要なアクセス許可がありません。

使用例使用例

次のコード例は、CustomAttributeBuilder を使用して、AssemblyBuilder 内の SetCustomAttribute使用する方法示してます。

<AttributeUsage(AttributeTargets.All, AllowMultiple := False)>
  _
Public Class MyAttribute
   Inherits Attribute
   Public s As String
   Public x As Integer

   Public Sub New(s As
 String, x As Integer)
      Me.s = s
      Me.x = x
   End Sub 'New
End Class 'MyAttribute

Class MyApplication
   Public Shared Sub Main()
      Dim customAttribute As Type = CreateCallee(Thread.GetDomain())
      Dim attributes As Object()
 = customAttribute.Assembly.GetCustomAttributes(True)
      Console.WriteLine("MyAttribute custom attribute contains
 : ")
      Dim index As Integer
      For index = 0 To attributes.Length -
 1
         If TypeOf attributes(index) Is
 MyAttribute Then
            Console.WriteLine("s : " + CType(attributes(index),
 MyAttribute).s)
            Console.WriteLine("x : " + CType(attributes(index),
 MyAttribute).x.ToString())
            Exit For
         End If
      Next index
   End Sub 'Main

   Private Shared Function
 CreateCallee(domain As AppDomain) As Type
      Dim myAssemblyName As New
 AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"
      Dim myAssembly As AssemblyBuilder = _
                     domain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
      Dim myType As Type = GetType(MyAttribute)
      Dim infoConstructor As ConstructorInfo
 = _
                     myType.GetConstructor(New Type(1) {GetType(String),
 GetType(Integer)})
      Dim attributeBuilder As New
 CustomAttributeBuilder(infoConstructor, New Object(1)
 {"Hello", 2})
      myAssembly.SetCustomAttribute(attributeBuilder)
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "HelloWorld" in the assembly.
      Dim helloWorldClass As TypeBuilder =
 myModule.DefineType("HelloWorld", TypeAttributes.Public)
      Return helloWorldClass.CreateType()
   End Function 'CreateCallee
End Class 'MyApplication
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public String s;
   public int x;

   public MyAttribute(String s, int x)
   {
      this.s = s;
      this.x = x;
   }
}

class MyApplication
{
   public static void Main()
   {
      Type customAttribute = CreateCallee(Thread.GetDomain());
      object[] attributes = customAttribute.Assembly.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains : ");
      for(int index=0; index < attributes.Length;
 index++)
      {
         if(attributes[index] is MyAttribute)
         {
            Console.WriteLine("s : " + ((MyAttribute)attributes[index]).s);
            Console.WriteLine("x : " + ((MyAttribute)attributes[index]).x);
            break;
         }
      }
   }


   private static Type CreateCallee(AppDomain
 domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
         AssemblyBuilderAccess.Run);
      Type myType = typeof(MyAttribute);
      ConstructorInfo infoConstructor = myType.GetConstructor(new
 Type[2]{typeof(String), typeof(int)});
      CustomAttributeBuilder attributeBuilder =
         new CustomAttributeBuilder(infoConstructor, new
 object[2]{"Hello", 2});
      myAssembly.SetCustomAttribute(attributeBuilder);
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "HelloWorld" in the assembly.
      TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);

      return(helloWorldClass.CreateType());
  }
}
[AttributeUsage(AttributeTargets::All,AllowMultiple=false)]
public ref class MyAttribute: public
 Attribute
{
public:
   String^ s;
   int x;
   MyAttribute( String^ s, int x )
   {
      this->s = s;
      this->x = x;
   }
};

Type^ CreateCallee( AppDomain^ domain )
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "EmittedAssembly";
   AssemblyBuilder^ myAssembly = domain->DefineDynamicAssembly( myAssemblyName,
 AssemblyBuilderAccess::Run );
   Type^ myType = MyAttribute::typeid;
   array<Type^>^temp0 = {String::typeid,int::typeid};
   ConstructorInfo^ infoConstructor = myType->GetConstructor( temp0 );
   array<Object^>^temp1 = {"Hello",2};
   CustomAttributeBuilder^ attributeBuilder = gcnew CustomAttributeBuilder( infoConstructor,temp1
 );
   myAssembly->SetCustomAttribute( attributeBuilder );
   ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule"
 );
   
   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder^ helloWorldClass = myModule->DefineType( "HelloWorld",
 TypeAttributes::Public );
   return (helloWorldClass->CreateType());
}

int main()
{
   Type^ customAttribute = CreateCallee( Thread::GetDomain() );
   array<Object^>^attributes = customAttribute->Assembly->GetCustomAttributes(
 true );
   Console::WriteLine( "MyAttribute custom attribute contains : " );
   for ( int index = 0; index < attributes->Length;
 index++ )
   {
      if ( dynamic_cast<MyAttribute^>(attributes[ index
 ]) )
      {
         Console::WriteLine( "s : {0}", (dynamic_cast<MyAttribute^>(attributes[
 index ]))->s );
         Console::WriteLine( "x : {0}", (dynamic_cast<MyAttribute^>(attributes[
 index ]))->x );
         break;
      }
   }
}
/** @attribute AttributeUsage(AttributeTargets.All, AllowMultiple = false)
 */
public class MyAttribute extends Attribute
{
    public String s;
    public int x;

    public MyAttribute(String s, int x)
    {
        this.s = s;
        this.x = x;
    } //MyAttribute
} //MyAttribute

class MyApplication
{
    public static void main(String[]
 args)
    {
        Type customAttribute = 
            CreateCallee(System.Threading.Thread.GetDomain());
        Object attributes[] = 
            customAttribute.get_Assembly().GetCustomAttributes(true);

        Console.WriteLine("MyAttribute custom attribute contains : ");
        for (int index = 0; index < attributes.length;
 index++) {
            if (attributes.get_Item(index) instanceof MyAttribute)
 {
                Console.WriteLine("s : " 
                    + ((MyAttribute)attributes.get_Item(index)).s);
                Console.WriteLine("x : " 
                    + ((MyAttribute)attributes.get_Item(index)).x);
                break;
            }
        }
    } //main

    private static Type CreateCallee(AppDomain
 domain) 
    {
        AssemblyName myAssemblyName =  new AssemblyName();
        myAssemblyName.set_Name("EmittedAssembly");

        AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(
            myAssemblyName, AssemblyBuilderAccess.Run);

        Type myType = MyAttribute.class.ToType();
        ConstructorInfo infoConstructor = myType.GetConstructor(
            new Type[]{String.class.ToType(),
 int.class.ToType()});

        CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(
            infoConstructor, new Object[]{"Hello", (Int32)2});
        myAssembly.SetCustomAttribute(attributeBuilder);

        ModuleBuilder myModule = 
            myAssembly.DefineDynamicModule("EmittedModule");

        // Define a public class named "HelloWorld" in the
 assembly.
        TypeBuilder helloWorldClass = 
            myModule.DefineType("HelloWorld", TypeAttributes.Public);
        return helloWorldClass.CreateType();
    } //CreateCallee
} //MyApplication
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間

AssemblyBuilder.SetCustomAttribute メソッド (ConstructorInfo, Byte[])

指定したカスタム属性 BLOB使用して、このアセンブリカスタム属性設定します

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

<ComVisibleAttribute(True)> _
Public Sub SetCustomAttribute ( _
    con As ConstructorInfo, _
    binaryAttribute As Byte() _
)
Dim instance As AssemblyBuilder
Dim con As ConstructorInfo
Dim binaryAttribute As Byte()

instance.SetCustomAttribute(con, binaryAttribute)
[ComVisibleAttribute(true)] 
public void SetCustomAttribute (
    ConstructorInfo con,
    byte[] binaryAttribute
)
[ComVisibleAttribute(true)] 
public:
void SetCustomAttribute (
    ConstructorInfo^ con, 
    array<unsigned char>^ binaryAttribute
)
/** @attribute ComVisibleAttribute(true) */ 
public void SetCustomAttribute (
    ConstructorInfo con, 
    byte[] binaryAttribute
)
ComVisibleAttribute(true) 
public function SetCustomAttribute (
    con : ConstructorInfo, 
    binaryAttribute : byte[]
)

パラメータ

con

カスタム属性用のコンストラクタ

binaryAttribute

属性を表すバイト BLOB

例外例外
例外種類条件

ArgumentNullException

con または binaryAttributenull 参照 (Visual Basic では Nothing) です。

SecurityException

呼び出し元に必要なアクセス許可がありません。

ArgumentException

conRuntimeConstructorInfo ではありません。

解説解説

binaryAttribute書式設定方法については、ECMA Partition IIマニュアルメタデータ仕様参照してくださいPartition IIマニュアルは、http://msdn.microsoft.com/net/ecma/http://msdn.microsoft.com/net/ecma/ および http://www.ecma-international.org/publications/standards/Ecma-335.htmhttp://www.ecma-international.org/publications/standards/Ecma-335.htm で入手できます

RuntimeConstructorInfo は、システム生成する特殊な型です。これは ConstructorInfo クラスから派生しリフレクションによって得られる ConstructorInfo オブジェクトは、実際にRuntimeConstructorInfoインスタンスです。ランタイム型の詳細については、「リフレクションランタイム型」を参照してください

使用例使用例

次のコード例は、SetCustomAttribute使用して動的に生成するアセンブリカスタム属性結び付ける方法示してます。

Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit


<AttributeUsage(AttributeTargets.All, AllowMultiple := False)>
  _
Public Class MyAttribute
   Inherits Attribute
   Public s As Boolean
   
   Public Sub New(s As
 Boolean)
      Me.s = s
   End Sub 'New
End Class 'MyAttribute

Class MyApplication
   
   Public Shared Sub Main()
      Dim customAttribute As Type = CreateCallee(Thread.GetDomain())
      Dim attributes As Object()
 = customAttribute.Assembly.GetCustomAttributes(True)
      Console.WriteLine("MyAttribute custom attribute contains
 : ")
      Dim index As Integer
      For index = 0 To attributes.Length -
 1
         If TypeOf attributes(index) Is
 MyAttribute Then
            Console.WriteLine("s : " + CType(attributes(index),
 MyAttribute).s.ToString())
            Exit For
         End If
      Next index
   End Sub 'Main
   
   Private Shared Function
 CreateCallee(domain As AppDomain) As Type
      Dim myAssemblyName As New
 AssemblyName()
      myAssemblyName.Name = "EmittedAssembly"
      Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly(myAssemblyName,
 _
                                                            AssemblyBuilderAccess.Run)
      Dim myType As Type = GetType(MyAttribute)
      Dim infoConstructor As ConstructorInfo
 = myType.GetConstructor(New Type() {GetType(Boolean)})
      myAssembly.SetCustomAttribute(infoConstructor, New Byte()
 {01, 00, 01})
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "HelloWorld" in the assembly.
      Dim helloWorldClass As TypeBuilder =
 myModule.DefineType("HelloWorld", TypeAttributes.Public)
      
      Return helloWorldClass.CreateType()
   End Function 'CreateCallee
End Class 'MyApplication
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;


[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public bool s;

   public MyAttribute(bool s)
   {
      this.s = s;
   }
}

class MyApplication
{
   public static void Main()
   {
      Type customAttribute = CreateCallee(Thread.GetDomain());
      object[] attributes = customAttribute.Assembly.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains : ");
      for(int index=0; index < attributes.Length;
 index++)
      {
         if(attributes[index] is MyAttribute)
         {
            Console.WriteLine("s : " + ((MyAttribute)attributes[index]).s);
            break;
         }
      }
   }

   private static Type CreateCallee(AppDomain
 domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
         AssemblyBuilderAccess.Run);
      Type myType = typeof(MyAttribute);
      ConstructorInfo infoConstructor = myType.GetConstructor(new
 Type[]{typeof(bool)});
      myAssembly.SetCustomAttribute(infoConstructor, new byte[]{01
,00,01});
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "HelloWorld" in the assembly.
      TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);

      return(helloWorldClass.CreateType());
  }
}
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

[AttributeUsage(AttributeTargets::All,AllowMultiple=false)]
public ref class MyAttribute: public
 Attribute
{
public:
   bool s;
   MyAttribute( bool s )
   {
      this->s = s;
   }
};

Type^ CreateCallee( AppDomain^ domain )
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName;
   myAssemblyName->Name = "EmittedAssembly";
   AssemblyBuilder^ myAssembly = domain->DefineDynamicAssembly( myAssemblyName,
 AssemblyBuilderAccess::Run );
   Type^ myType = MyAttribute::typeid;
   array<Type^>^temp0 = {bool::typeid};
   ConstructorInfo^ infoConstructor = myType->GetConstructor( temp0 );
   array<Byte>^temp1 = {01,00,01};
   myAssembly->SetCustomAttribute( infoConstructor, temp1 );
   ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule"
 );

   // Define a public class named "HelloWorld" in the assembly.
   TypeBuilder^ helloWorldClass = myModule->DefineType( "HelloWorld",
 TypeAttributes::Public );
   return (helloWorldClass->CreateType());
}

int main()
{
   Type^ customAttribute = CreateCallee( Thread::GetDomain() );
   array<Object^>^attributes = customAttribute->Assembly->GetCustomAttributes(
 true );
   Console::WriteLine( "MyAttribute custom attribute contains : " );
   for ( int index = 0; index < attributes->Length;
 index++ )
   {
      if ( dynamic_cast<MyAttribute^>(attributes[ index
 ]) )
      {
         Console::WriteLine( "s : {0}", (dynamic_cast<MyAttribute^>(attributes[
 index ]))->s );
         break;
      }
   }
}
import System.*;
import System.Threading.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

/** @attribute AttributeUsage(AttributeTargets.All, AllowMultiple = false)
 */
public class MyAttribute extends Attribute
{
    public boolean s;

    public MyAttribute(boolean s)
    {
        this.s = s;
    } //MyAttribute
} //MyAttribute

class MyApplication
{
    public static void main(String[]
 args)
    {
        Type customAttribute = 
            CreateCallee(System.Threading.Thread.GetDomain());
            
        Object attributes[] = 
            customAttribute.get_Assembly().GetCustomAttributes(true);

        Console.WriteLine("MyAttribute custom attribute contains : ");
        for (int index = 0; index < attributes.length;
 index++) {
            if (attributes.get_Item(index) instanceof MyAttribute)
 {
                Console.WriteLine("s : " + (System.Boolean)
                    ((MyAttribute)attributes.get_Item(index)).s);
                break;
            }
        }
    } //main

    private static Type CreateCallee(AppDomain
 domain)
    {
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.set_Name("EmittedAssembly");

        AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(
            myAssemblyName, AssemblyBuilderAccess.Run);
        Type myType = MyAttribute.class.ToType();

        ConstructorInfo infoConstructor = myType.GetConstructor(new
 Type[] 
            { boolean.class.ToType() });
        myAssembly.SetCustomAttribute(infoConstructor, new ubyte[]
 { 1, 0, 1 });

        ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");

        // Define a public class named "HelloWorld" in the
 assembly.
        TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld",
 
            TypeAttributes.Public);
        return helloWorldClass.CreateType();
    } //CreateCallee
} //MyApplication
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間

AssemblyBuilder.SetCustomAttribute メソッド

このアセンブリカスタム属性設定します
オーバーロードの一覧オーバーロードの一覧

名前 説明
AssemblyBuilder.SetCustomAttribute (CustomAttributeBuilder) カスタム属性ビルダ使用して、このアセンブリカスタム属性設定します
AssemblyBuilder.SetCustomAttribute (ConstructorInfo, Byte[]) 指定したカスタム属性 BLOB使用して、このアセンブリカスタム属性設定します
参照参照

関連項目

AssemblyBuilder クラス
AssemblyBuilder メンバ
System.Reflection.Emit 名前空間



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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2024 GRAS Group, Inc.RSS