Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets a custom attribute on this assembly.
SetCustomAttribute(CustomAttributeBuilder) |
Set a custom attribute on this assembly using a custom attribute builder. |
SetCustomAttribute(ConstructorInfo, Byte[]) |
Set a custom attribute on this assembly using a specified custom attribute blob. |
Set a custom attribute on this assembly using a custom attribute builder.
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)
An instance of a helper class to define the custom attribute.
customBuilder
is null
.
The caller does not have the required permission.
The following code sample illustrates the use of SetCustomAttribute
within AssemblyBuilder, using a CustomAttributeBuilder.
[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
Note
SetCustomAttribute cannot be used to set declarative security attributes. Use one of the overloads of DefineDynamicAssembly that takes required, optional, and refused permissions.
Product | Versions |
---|---|
.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 |
Set a custom attribute on this assembly using a specified custom attribute 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())
The constructor for the custom attribute.
A byte blob representing the attributes.
con
or binaryAttribute
is null
.
The caller does not have the required permission.
con
is not a RuntimeConstructorInfo
object.
The following code sample illustrates the use of SetCustomAttribute
to attach a custom attribute to a dynamically generated assembly.
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
For information on how to format binaryAttribute
, see the metadata specification in Partition II of the Common Language Infrastructure (CLI) specification.
RuntimeConstructorInfo
is a special type generated by the system. It derives from the ConstructorInfo class, and any ConstructorInfo object you obtain through reflection is actually an instance of RuntimeConstructorInfo
.
Note
SetCustomAttribute cannot be used to set declarative security attributes. Use one of the overloads of DefineDynamicAssembly that takes required, optional, and refused permissions.
Product | Versions |
---|---|
.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 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in