ConstructorBuilder.SetCustomAttribute Método

Definición

Establece un atributo personalizado.

Sobrecargas

SetCustomAttribute(CustomAttributeBuilder)

Establece un atributo personalizado mediante un generador de atributos personalizado.

SetCustomAttribute(ConstructorInfo, Byte[])

Establece un atributo personalizado mediante un blob de atributo personalizado especificado.

SetCustomAttribute(CustomAttributeBuilder)

Source:
ConstructorBuilder.cs
Source:
ConstructorBuilder.cs
Source:
ConstructorBuilder.cs

Establece un atributo personalizado mediante un generador de atributos personalizado.

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

Parámetros

customBuilder
CustomAttributeBuilder

Instancia de una clase del asistente para definir el atributo personalizado.

Excepciones

customBuilder es null.

Ejemplos

En el ejemplo de código siguiente se muestra el uso del SetCustomAttribute contexto de , ConstructorBuilderpasando un CustomAttributeBuilder.

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
   public String myString;
   public int myInteger;
   public MyAttribute(String myString, int myInteger)
   {
      this.myString = myString;
      this.myInteger = myInteger;
   }
}

public class MyConstructorBuilder
{
   public static void Main()
   {
      Type myHelloworld = MyCreateCallee(Thread.GetDomain());
      ConstructorInfo myConstructor = myHelloworld.GetConstructor(new Type[]{typeof(String)});
      object[] myAttributes1 = myConstructor.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains  ");
      for(int index=0; index < myAttributes1.Length; index++)
      {
         if(myAttributes1[index] is MyAttribute)
         {
            Console.WriteLine("The value of myString is : "
                                       + ((MyAttribute)myAttributes1[index]).myString);
            Console.WriteLine("The value of myInteger is : "
                                       + ((MyAttribute)myAttributes1[index]).myInteger);
         }
      }
   }

   private static Type MyCreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssembly =
                  domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
       // Construct a 'TypeBuilder' given the name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
         TypeAttributes.Public);
      // Define a constructor of the dynamic class.
      ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
               MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
      ILGenerator myILGenerator = myConstructor.GetILGenerator();
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
      myILGenerator.Emit(OpCodes.Ldarg_1);
      MethodInfo myMethodInfo =
                     typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
      myILGenerator.Emit(OpCodes.Call, myMethodInfo);
      myILGenerator.Emit(OpCodes.Ret);
      Type myType = typeof(MyAttribute);
      ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[2]{typeof(String), typeof(int)});
      CustomAttributeBuilder attributeBuilder =
         new CustomAttributeBuilder(myConstructorInfo, new object[2]{"Hello", 2});
      try
      {
         myConstructor.SetCustomAttribute(attributeBuilder);
      }
      catch(ArgumentNullException ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      catch(Exception ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      return myTypeBuilder.CreateType();
   }
}

Se aplica a

.NET 9 y otras versiones
Producto Versiones
.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
.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, 2.1

SetCustomAttribute(ConstructorInfo, Byte[])

Source:
ConstructorBuilder.cs
Source:
ConstructorBuilder.cs
Source:
ConstructorBuilder.cs

Establece un atributo personalizado mediante un blob de atributo personalizado especificado.

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

Parámetros

con
ConstructorInfo

Constructor para el atributo personalizado.

binaryAttribute
Byte[]

Blob de bytes que representa los atributos.

Atributos

Excepciones

con o binaryAttribute es null.

Ejemplos

En el ejemplo de código siguiente se muestra el uso del SetCustomAttribute contexto de , ConstructorBuilderpasando un blob de bytes.

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

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

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

public class MyConstructorBuilder
{
   public static void Main()
   {
      Type myHelloworld = MyCreateCallee(Thread.GetDomain());
      ConstructorInfo myConstructor = myHelloworld.GetConstructor(new Type[]{typeof(String)});
      object[] myAttributes1 = myConstructor.GetCustomAttributes(true);
      Console.WriteLine("MyAttribute custom attribute contains  ");
      for(int index=0; index < myAttributes1.Length; index++)
      {
         if(myAttributes1[index] is MyAttribute)
         {
            Console.WriteLine("myBoolean : " + ((MyAttribute)myAttributes1[index]).myBoolean);
         }
      }
   }

   private static Type MyCreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssembly =
                  domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
      // Construct a 'TypeBuilder' given the name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
         TypeAttributes.Public);
      // Define a constructor of the dynamic class.
      ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
               MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
      ILGenerator myILGenerator = myConstructor.GetILGenerator();
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
      myILGenerator.Emit(OpCodes.Ldarg_1);
      MethodInfo myMethodInfo =
                     typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
      myILGenerator.Emit(OpCodes.Call, myMethodInfo);
      myILGenerator.Emit(OpCodes.Ret);
      Type myType = typeof(MyAttribute);
      ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[]{typeof(bool)});
      try
      {
         myConstructor.SetCustomAttribute(myConstructorInfo, new byte[]{01,00,01});
      }
      catch(ArgumentNullException ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      catch(Exception ex)
      {
         Console.WriteLine("The following exception has occurred : "+ex.Message);
      }
      return myTypeBuilder.CreateType();
   }
}

Comentarios

Para obtener información sobre cómo dar formato binaryAttributea , consulte la especificación de metadatos en la partición II de la especificación de Common Language Infrastructure (CLI).

Se aplica a

.NET 9 y otras versiones
Producto Versiones
.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
.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, 2.1