AssemblyBuilder.SetCustomAttribute 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在此程序集上设置自定义属性。
重载
| SetCustomAttribute(CustomAttributeBuilder) | 使用自定义属性生成器设置此程序集的自定义属性。 | 
| SetCustomAttribute(ConstructorInfo, Byte[]) | 使用指定的自定义属性 blob 在此程序集上设置自定义属性。 | 
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 -> unitPublic Sub SetCustomAttribute (customBuilder As CustomAttributeBuilder)参数
- customBuilder
- CustomAttributeBuilder
用于定义自定义属性的帮助程序类的一个实例。
例外
              con 为 null。
调用方没有所要求的权限。
示例
下面的代码示例演示如何在 CustomAttributeBuilder中使用 SetCustomAttributeAssemblyBuilder。
[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 Service Pack 1 开始,此成员不再需要ReflectionPermission带有 ReflectionPermissionFlag.ReflectionEmit 标志。 (请参阅 Reflection Emit.) 中的安全问题若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。
适用于
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[] -> unitPublic Sub SetCustomAttribute (con As ConstructorInfo, binaryAttribute As Byte())参数
- con
- ConstructorInfo
自定义属性的构造函数。
- binaryAttribute
- Byte[]
表示属性的字节 blob。
- 属性
例外
              con 或 binaryAttribute 为 null。
调用方没有所要求的权限。
              con 不是 RuntimeConstructorInfo 对象。
示例
下面的代码示例演示如何使用 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的信息,请参阅 公共语言基础结构的分区 II (CLI) 规范中的元数据规范。
              RuntimeConstructorInfo 是系统生成的特殊类型。 它派生自 ConstructorInfo 类,通过反射获取的任何 ConstructorInfo 对象实际上是 的 RuntimeConstructorInfo实例。
注意
SetCustomAttribute 不能用于设置声明性安全属性。 使用采用必需、可选和拒绝权限的 的 DefineDynamicAssembly 重载之一。
注意
从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要ReflectionPermission带有 ReflectionPermissionFlag.ReflectionEmit 标志。 (请参阅 Reflection Emit.) 中的安全问题若要使用此功能,应用程序应面向 .NET Framework 3.5 或更高版本。