public SetCustomAttribute ( System customBuilder ) : void | ||
customBuilder | System | |
return | void |
using System; using System.Reflection; using System.Reflection.Emit; public class Program { public static void Main(string[] args) { // create a new dynamic assembly AssemblyName assemblyName = new AssemblyName("MyAssembly"); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); // create a new module in the assembly ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule"); // create a new type in the module TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType"); // define a new method in the type MethodBuilder methodBuilder = typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { }); // create a new custom attribute builder object Type attributeType = typeof(ObsoleteAttribute); ConstructorInfo attributeConstructor = attributeType.GetConstructor(new Type[] { typeof(string) }); CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { "This method is obsolete." }); // set the custom attribute on the method methodBuilder.SetCustomAttribute(attributeBuilder); // create the type and save the assembly Type type = typeBuilder.CreateType(); assemblyBuilder.Save("MyAssembly.dll"); } }
using System; using System.Reflection; using System.Reflection.Emit; public class Program { public static void Main(string[] args) { // create a new dynamic assembly AssemblyName assemblyName = new AssemblyName("MyAssembly"); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave); // create a new module in the assembly ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule"); // create a new type in the module TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType"); // define a new field in the type FieldBuilder fieldBuilder = typeBuilder.DefineField("MyField", typeof(int), FieldAttributes.Public | FieldAttributes.Static); // create a new custom attribute builder object Type attributeType = typeof(ObsoleteAttribute); ConstructorInfo attributeConstructor = attributeType.GetConstructor(new Type[] { typeof(string) }); CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { "This field is obsolete." }); // set the custom attribute on the field fieldBuilder.SetCustomAttribute(attributeBuilder); // create the type and save the assembly Type type = typeBuilder.CreateType(); assemblyBuilder.Save("MyAssembly.dll"); } }