英語で読む

次の方法で共有


AssemblyBuilder.SetCustomAttribute メソッド

定義

重要

一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。

このアセンブリにカスタム属性を設定します。

オーバーロード

SetCustomAttribute(CustomAttributeBuilder)

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

SetCustomAttribute(ConstructorInfo, Byte[])

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

SetCustomAttribute(CustomAttributeBuilder)

ソース:
AssemblyBuilder.cs
ソース:
AssemblyBuilder.cs
ソース:
AssemblyBuilder.cs

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

C#
public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder);

パラメーター

customBuilder
CustomAttributeBuilder

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

例外

connullです。

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

次のコード サンプルは、 を使用した 内AssemblyBuilderでの のSetCustomAttribute使用をCustomAttributeBuilder示しています。

C#
[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());
  }
}

注釈

注意

SetCustomAttribute を使用して宣言型セキュリティ属性を設定することはできません。 必須、省略可能、拒否されたアクセス許可を受け取る のオーバーロード DefineDynamicAssembly のいずれかを使用します。

注意

.NET Framework 2.0 Service Pack 1 以降では、このメンバーは フラグをReflectionPermissionFlag.ReflectionEmit使用する必要ReflectionPermissionがなくなりました。 (リフレクション出力のセキュリティの問題に関するページを参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5 以降をターゲットにする必要があります。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1

SetCustomAttribute(ConstructorInfo, Byte[])

ソース:
AssemblyBuilder.cs
ソース:
AssemblyBuilder.cs
ソース:
AssemblyBuilder.cs

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

C#
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute);

パラメーター

con
ConstructorInfo

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

binaryAttribute
Byte[]

属性を表すバイト blob。

属性

例外

con または binaryAttributenull です。

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

conRuntimeConstructorInfo オブジェクトではありません。

次のコード サンプルは、 を使用して、動的に生成されたアセンブリにカスタム属性をアタッチする方法 SetCustomAttribute を示しています。

C#
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());
  }
}

注釈

の書式を設定 binaryAttributeする方法については、 共通言語インフラストラクチャ (CLI) 仕様のパーティション II のメタデータ仕様を参照してください。

RuntimeConstructorInfo は、システムによって生成される特殊な型です。 クラスから ConstructorInfo 派生し、リフレクションによって取得する ConstructorInfo オブジェクトは実際には の RuntimeConstructorInfoインスタンスです。

注意

SetCustomAttribute を使用して宣言型セキュリティ属性を設定することはできません。 必須、省略可能、拒否されたアクセス許可を受け取る のオーバーロード DefineDynamicAssembly のいずれかを使用します。

注意

.NET Framework 2.0 Service Pack 1 以降では、このメンバーは フラグをReflectionPermissionFlag.ReflectionEmit使用する必要ReflectionPermissionがなくなりました。 (リフレクション出力のセキュリティの問題に関するページを参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5 以降をターゲットにする必要があります。

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided), 2.1