AssemblyBuilder.SetCustomAttribute 메서드

정의

해당 어셈블리에 사용자 지정 특성을 설정합니다.

오버로드

SetCustomAttribute(ConstructorInfo, Byte[])

지정한 사용자 지정 특성 blob을 사용하여 이 어셈블리에 대한 사용자 지정 특성을 설정합니다.

SetCustomAttribute(CustomAttributeBuilder)

사용자 지정 특성 작성기를 사용하여 이 어셈블리에 대한 사용자 지정 특성을 설정합니다.

SetCustomAttribute(ConstructorInfo, Byte[])

Source:
AssemblyBuilder.cs
Source:
AssemblyBuilder.cs
Source:
AssemblyBuilder.cs

지정한 사용자 지정 특성 blob을 사용하여 이 어셈블리에 대한 사용자 지정 특성을 설정합니다.

public:
 void SetCustomAttribute(System::Reflection::ConstructorInfo ^ con, cli::array <System::Byte> ^ binaryAttribute);
public void SetCustomAttribute (System.Reflection.ConstructorInfo con, byte[] binaryAttribute);
[System.Runtime.InteropServices.ComVisible(true)]
public void SetCustomAttribute (System.Reflection.ConstructorInfo con, byte[] binaryAttribute);
member this.SetCustomAttribute : System.Reflection.ConstructorInfo * byte[] -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.SetCustomAttribute : System.Reflection.ConstructorInfo * byte[] -> unit
Public Sub SetCustomAttribute (con As ConstructorInfo, binaryAttribute As Byte())

매개 변수

con
ConstructorInfo

사용자 지정 특성에 대한 생성자입니다.

binaryAttribute
Byte[]

특성을 나타내는 바이트 blob입니다.

특성

예외

con 또는 binaryAttributenull인 경우

호출자에게 필요한 권한이 없는 경우

conRuntimeConstructorInfo 개체가 아닙니다.

예제

다음 코드 샘플에서는 를 사용하여 SetCustomAttribute 동적으로 생성된 어셈블리에 사용자 지정 특성을 연결하는 방법을 보여 줍니다.

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;
      }
   }
}
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());
  }
}
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
End Class

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
   
   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

설명

형식 binaryAttribute을 지정하는 방법에 대한 자세한 내용은 CLI(공용 언어 인프라) 사양의 파티션 II의 메타데이터 사양을 참조하세요.

RuntimeConstructorInfo 는 시스템에서 생성된 특수 형식입니다. 클래스에서 파생되며 리플렉션을 ConstructorInfo 통해 가져오는 모든 ConstructorInfo 개체는 실제로 의 RuntimeConstructorInfoinstance.

참고

SetCustomAttribute 는 선언적 보안 특성을 설정하는 데 사용할 수 없습니다. 필수, 선택 사항 및 거부된 권한을 사용하는 의 DefineDynamicAssembly 오버로드 중 하나를 사용합니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상

SetCustomAttribute(CustomAttributeBuilder)

Source:
AssemblyBuilder.cs
Source:
AssemblyBuilder.cs
Source:
AssemblyBuilder.cs

사용자 지정 특성 작성기를 사용하여 이 어셈블리에 대한 사용자 지정 특성을 설정합니다.

public:
 void SetCustomAttribute(System::Reflection::Emit::CustomAttributeBuilder ^ customBuilder);
public void SetCustomAttribute (System.Reflection.Emit.CustomAttributeBuilder customBuilder);
member this.SetCustomAttribute : System.Reflection.Emit.CustomAttributeBuilder -> unit
Public Sub SetCustomAttribute (customBuilder As CustomAttributeBuilder)

매개 변수

customBuilder
CustomAttributeBuilder

사용자 지정 특성을 정의하는 도우미 클래스의 인스턴스입니다.

예외

con이(가) null인 경우

호출자에게 필요한 권한이 없는 경우

예제

다음 코드 샘플에서는 를 사용하여 내에서 AssemblyBuilderSetCustomAttribute 사용하는 것을 보여 줍니다CustomAttributeBuilder.

[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;
      }
   }
}
[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 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
End Class

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

   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

설명

참고

SetCustomAttribute 는 선언적 보안 특성을 설정하는 데 사용할 수 없습니다. 필수, 선택 사항 및 거부된 권한을 사용하는 의 DefineDynamicAssembly 오버로드 중 하나를 사용합니다.

참고

.NET Framework 2.0 서비스 팩 1부터 이 멤버는 ReflectionPermission 더 이상 플래그가 ReflectionPermissionFlag.ReflectionEmit 필요하지 않습니다. (리플렉션 내보내기의 보안 문제를 참조하세요.) 이 기능을 사용하려면 애플리케이션이 .NET Framework 3.5 이상을 대상으로 해야 합니다.

적용 대상