ConstructorBuilder.SetCustomAttribute Metodo
In questo articolo
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Imposta un attributo personalizzato.
SetCustomAttribute(CustomAttributeBuilder) |
Impostare un attributo personalizzato usando un generatore di attributi personalizzati. |
SetCustomAttribute(ConstructorInfo, Byte[]) |
Imposta un attributo personalizzato usando un BLOB di attributi personalizzati specificato. |
- Origine:
- ConstructorBuilder.cs
- Origine:
- ConstructorBuilder.cs
- Origine:
- ConstructorBuilder.cs
Impostare un attributo personalizzato usando un generatore di attributi personalizzati.
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)
Parametri
- customBuilder
- CustomAttributeBuilder
Un'istanza di una classe helper per definire l'attributo personalizzato.
Eccezioni
customBuilder
è null
.
Esempio
Nell'esempio di codice seguente viene illustrato l'uso SetCustomAttribute
del contesto di un ConstructorBuilderoggetto , passando un oggetto CustomAttributeBuilder.
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:
String^ myString;
int myInteger;
MyAttribute( String^ myString, int myInteger )
{
this->myString = myString;
this->myInteger = myInteger;
}
};
static Type^ MyCreateCallee( AppDomain^ domain )
{
AssemblyName^ myAssemblyName = gcnew 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.
array<Type^>^type1 = {String::typeid};
ConstructorBuilder^ myConstructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, type1 );
ILGenerator^ myILGenerator = myConstructor->GetILGenerator();
myILGenerator->Emit( OpCodes::Ldstr, "Constructor is invoked" );
myILGenerator->Emit( OpCodes::Ldarg_1 );
array<Type^>^type2 = {String::typeid};
MethodInfo^ myMethodInfo = Console::typeid->GetMethod( "WriteLine", type2 );
myILGenerator->Emit( OpCodes::Call, myMethodInfo );
myILGenerator->Emit( OpCodes::Ret );
Type^ myType = MyAttribute::typeid;
array<Type^>^type3 = {String::typeid,int::typeid};
ConstructorInfo^ myConstructorInfo = myType->GetConstructor( type3 );
array<Object^>^obj1 = {"Hello",2};
CustomAttributeBuilder^ attributeBuilder = gcnew CustomAttributeBuilder( myConstructorInfo,obj1 );
try
{
myConstructor->SetCustomAttribute( attributeBuilder );
}
catch ( ArgumentNullException^ ex )
{
Console::WriteLine( "The following exception has occurred : {0}", ex->Message );
}
catch ( Exception^ ex )
{
Console::WriteLine( "The following exception has occurred : {0}", ex->Message );
}
return myTypeBuilder->CreateType();
}
int main()
{
Type^ myHelloworld = MyCreateCallee( Thread::GetDomain() );
array<Type^>^type1 = {String::typeid};
ConstructorInfo^ myConstructor = myHelloworld->GetConstructor( type1 );
array<Object^>^myAttributes1 = myConstructor->GetCustomAttributes( true );
Console::WriteLine( "MyAttribute custom attribute contains " );
for ( int index = 0; index < myAttributes1->Length; index++ )
{
if ( dynamic_cast<MyAttribute^>(myAttributes1[ index ]) )
{
Console::WriteLine( "The value of myString is : {0}", (safe_cast<MyAttribute^>(myAttributes1[ index ]))->myString );
Console::WriteLine( "The value of myInteger is : {0}", (safe_cast<MyAttribute^>(myAttributes1[ index ]))->myInteger );
}
}
}
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();
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)> _
Public Class MyAttribute
Inherits Attribute
Public myString As String
Public myInteger As Integer
Public Sub New(myString As String, myInteger As Integer)
Me.myString = myString
Me.myInteger = myInteger
End Sub
End Class
Public Class MyConstructorBuilder
Public Shared Sub Main()
Dim myHelloworld As Type = MyCreateCallee(Thread.GetDomain())
Dim myConstructor As ConstructorInfo = myHelloworld.GetConstructor(New Type() _
{GetType(String)})
Dim myAttributes1 As Object() = myConstructor.GetCustomAttributes(True)
Console.WriteLine("MyAttribute custom attribute contains ")
Dim index As Integer
For index = 0 To myAttributes1.Length - 1
If TypeOf myAttributes1(index) Is MyAttribute Then
Console.WriteLine("The value of myString is : " + CType(myAttributes1(index), _
MyAttribute).myString)
Console.WriteLine("The value of myInteger is : " + CType(myAttributes1(index), _
MyAttribute).myInteger.ToString())
End If
Next index
End Sub
Private Shared Function MyCreateCallee(domain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "EmittedAssembly"
' Define a dynamic assembly in the current application domain.
Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly _
(myAssemblyName, AssemblyBuilderAccess.Run)
' Define a dynamic module in this assembly.
Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Construct a 'TypeBuilder' given the name and attributes.
Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType _
("HelloWorld", TypeAttributes.Public)
' Define a constructor of the dynamic class.
Dim myConstructor As ConstructorBuilder = myTypeBuilder.DefineConstructor _
(MethodAttributes.Public, CallingConventions.Standard, New Type() {GetType(String)})
Dim myILGenerator As ILGenerator = myConstructor.GetILGenerator()
myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked")
myILGenerator.Emit(OpCodes.Ldarg_1)
Dim myMethodInfo As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() _
{GetType(String)})
myILGenerator.Emit(OpCodes.Call, myMethodInfo)
myILGenerator.Emit(OpCodes.Ret)
Dim myType As Type = GetType(MyAttribute)
Dim myConstructorInfo As ConstructorInfo = myType.GetConstructor(New Type(1) _
{GetType(String), GetType(Integer)})
Dim attributeBuilder As New CustomAttributeBuilder _
(myConstructorInfo, New Object(1) {"Hello", 2})
Try
myConstructor.SetCustomAttribute(attributeBuilder)
Catch ex As ArgumentNullException
Console.WriteLine("The following exception has occurred : " + ex.Message)
Catch ex As Exception
Console.WriteLine("The following exception has occurred : " + ex.Message)
End Try
Return myTypeBuilder.CreateType()
End Function 'MyCreateCallee
End Class
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.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 |
- Origine:
- ConstructorBuilder.cs
- Origine:
- ConstructorBuilder.cs
- Origine:
- ConstructorBuilder.cs
Imposta un attributo personalizzato usando un BLOB di attributi personalizzati specificato.
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())
Parametri
- con
- ConstructorInfo
Costruttore dell'attributo personalizzato.
- binaryAttribute
- Byte[]
BLOB di byte che rappresenta gli attributi.
- Attributi
Eccezioni
con
o binaryAttribute
è null
.
Esempio
Nell'esempio di codice seguente viene illustrato l'uso del SetCustomAttribute
contesto di un ConstructorBuilderoggetto , passando un BLOB di byte.
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 myBoolean;
MyAttribute( bool myBoolean )
{
this->myBoolean = myBoolean;
}
};
static Type^ MyCreateCallee( AppDomain^ domain )
{
AssemblyName^ myAssemblyName = gcnew 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.
array<Type^>^type1 = {String::typeid};
ConstructorBuilder^ myConstructor = myTypeBuilder->DefineConstructor( MethodAttributes::Public, CallingConventions::Standard, type1 );
ILGenerator^ myILGenerator = myConstructor->GetILGenerator();
myILGenerator->Emit( OpCodes::Ldstr, "Constructor is invoked" );
myILGenerator->Emit( OpCodes::Ldarg_1 );
array<Type^>^type2 = {String::typeid};
MethodInfo^ myMethodInfo = Console::typeid->GetMethod( "WriteLine", type2 );
myILGenerator->Emit( OpCodes::Call, myMethodInfo );
myILGenerator->Emit( OpCodes::Ret );
Type^ myType = MyAttribute::typeid;
array<Type^>^type3 = {bool::typeid};
ConstructorInfo^ myConstructorInfo = myType->GetConstructor( type3 );
try
{
array<Byte>^bytes = {01,00,01};
myConstructor->SetCustomAttribute( myConstructorInfo, bytes );
}
catch ( ArgumentNullException^ ex )
{
Console::WriteLine( "The following exception has occurred : {0}", ex->Message );
}
catch ( Exception^ ex )
{
Console::WriteLine( "The following exception has occurred : {0}", ex->Message );
}
return myTypeBuilder->CreateType();
}
int main()
{
Type^ myHelloworld = MyCreateCallee( Thread::GetDomain() );
array<Type^>^type1 = {String::typeid};
ConstructorInfo^ myConstructor = myHelloworld->GetConstructor( type1 );
array<Object^>^myAttributes1 = myConstructor->GetCustomAttributes( true );
Console::WriteLine( "MyAttribute custom attribute contains " );
for ( int index = 0; index < myAttributes1->Length; index++ )
{
if ( dynamic_cast<MyAttribute^>(myAttributes1[ index ]) )
{
Console::WriteLine( "myBoolean : {0}", safe_cast<MyAttribute^>(myAttributes1[ index ])->myBoolean );
}
}
}
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();
}
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)> _
Public Class MyAttribute
Inherits Attribute
Public myBoolean As Boolean
Public Sub New(myBoolean As Boolean)
Me.myBoolean = myBoolean
End Sub
End Class
Public Class MyConstructorBuilder
Public Shared Sub Main()
Dim myHelloworld As Type = MyCreateCallee(Thread.GetDomain())
Dim myConstructor As ConstructorInfo = myHelloworld.GetConstructor(New Type() _
{GetType(String)})
Dim myAttributes1 As Object() = myConstructor.GetCustomAttributes(True)
Console.WriteLine("MyAttribute custom attribute contains ")
Dim index As Integer
For index = 0 To myAttributes1.Length - 1
If TypeOf myAttributes1(index) Is MyAttribute Then
Console.WriteLine("myBoolean : " + _
CType(myAttributes1(index), MyAttribute).myBoolean.ToString())
End If
Next index
End Sub
Private Shared Function MyCreateCallee(domain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "EmittedAssembly"
' Define a dynamic assembly in the current application domain.
Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly _
(myAssemblyName, AssemblyBuilderAccess.Run)
' Define a dynamic module in this assembly.
Dim myModuleBuilder As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Construct a 'TypeBuilder' given the name and attributes.
Dim myTypeBuilder As TypeBuilder = myModuleBuilder.DefineType _
("HelloWorld", TypeAttributes.Public)
' Define a constructor of the dynamic class.
Dim myConstructor As ConstructorBuilder = myTypeBuilder.DefineConstructor _
(MethodAttributes.Public, CallingConventions.Standard, New Type() {GetType(String)})
Dim myILGenerator As ILGenerator = myConstructor.GetILGenerator()
myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked")
myILGenerator.Emit(OpCodes.Ldarg_1)
Dim myMethodInfo As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() _
{GetType(String)})
myILGenerator.Emit(OpCodes.Call, myMethodInfo)
myILGenerator.Emit(OpCodes.Ret)
Dim myType As Type = GetType(MyAttribute)
Dim myConstructorInfo As ConstructorInfo = myType.GetConstructor(New Type() {GetType(Boolean)})
Try
myConstructor.SetCustomAttribute(myConstructorInfo, New Byte() {1, 0, 1})
Catch ex As ArgumentNullException
Console.WriteLine("The following exception has occurred : " + ex.Message)
Catch ex As Exception
Console.WriteLine("The following exception has occurred : " + ex.Message)
End Try
Return myTypeBuilder.CreateType()
End Function 'MyCreateCallee
End Class
Commenti
Per informazioni su come formattare binaryAttribute
, vedere la specifica dei metadati nella partizione II della specifica Common Language Infrastructure (CLI).
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.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 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: