AssemblyBuilder.SetCustomAttribute Method (CustomAttributeBuilder)
.NET Framework 2.0
Set a custom attribute on this assembly using a custom attribute builder.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
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()); } }
/** @attribute AttributeUsage(AttributeTargets.All, AllowMultiple = false)
*/
public class MyAttribute extends Attribute
{
public String s;
public int x;
public MyAttribute(String s, int x)
{
this.s = s;
this.x = x;
} //MyAttribute
} //MyAttribute
class MyApplication
{
public static void main(String[] args)
{
Type customAttribute =
CreateCallee(System.Threading.Thread.GetDomain());
Object attributes[] =
customAttribute.get_Assembly().GetCustomAttributes(true);
Console.WriteLine("MyAttribute custom attribute contains : ");
for (int index = 0; index < attributes.length; index++) {
if (attributes.get_Item(index) instanceof MyAttribute) {
Console.WriteLine("s : "
+ ((MyAttribute)attributes.get_Item(index)).s);
Console.WriteLine("x : "
+ ((MyAttribute)attributes.get_Item(index)).x);
break;
}
}
} //main
private static Type CreateCallee(AppDomain domain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.set_Name("EmittedAssembly");
AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(
myAssemblyName, AssemblyBuilderAccess.Run);
Type myType = MyAttribute.class.ToType();
ConstructorInfo infoConstructor = myType.GetConstructor(
new Type[]{String.class.ToType(), int.class.ToType()});
CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(
infoConstructor, new Object[]{"Hello", (Int32)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();
} //CreateCallee
} //MyApplication
- ReflectionPermission SecurityAction.Demand, ReflectionEmit=true
- ReflectionPermission when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess.
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.