Mega Code Archive

 
Categories / C# / Reflection
 

Defines a company name custom attribute for an assembly manifest

using System; using System.Reflection; using System.Reflection.Emit; class Example {    public static void Main()    {       AssemblyName assemName = new AssemblyName();       assemName.Name = "EmittedAssembly";       AssemblyBuilder myAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Save);       Type attributeType = typeof(AssemblyFileVersionAttribute);       Type[] ctorParameters = { typeof(string) };       ConstructorInfo ctor = attributeType.GetConstructor(ctorParameters);       object[] ctorArgs = { "1.1.1111.0" };       CustomAttributeBuilder attribute = new CustomAttributeBuilder(ctor, ctorArgs);       myAssembly.SetCustomAttribute(attribute);       attributeType = typeof(AssemblyTitleAttribute);       ctor = attributeType.GetConstructor(ctorParameters);       ctorArgs = new object[] { "Title" };       attribute = new CustomAttributeBuilder(ctor, ctorArgs);       myAssembly.SetCustomAttribute(attribute);       attributeType = typeof(AssemblyCopyrightAttribute);       ctor = attributeType.GetConstructor(ctorParameters);       ctorArgs = new object[] { "Copyright 2010-2011" };       attribute = new CustomAttributeBuilder(ctor, ctorArgs);       myAssembly.SetCustomAttribute(attribute);       attributeType = typeof(AssemblyDescriptionAttribute);       ctor = attributeType.GetConstructor(ctorParameters);       attribute = new CustomAttributeBuilder(ctor, new object[] { "a comment." });       myAssembly.SetCustomAttribute(attribute);       attributeType = typeof(AssemblyCompanyAttribute);       ctor = attributeType.GetConstructor(ctorParameters);       attribute = new CustomAttributeBuilder(ctor, new object[] { "My Example Company" });       myAssembly.SetCustomAttribute(attribute);       attributeType = typeof(AssemblyProductAttribute);       ctor = attributeType.GetConstructor(ctorParameters);       attribute = new CustomAttributeBuilder(ctor, new object[] { "My Product Name" });       myAssembly.SetCustomAttribute(attribute);       ModuleBuilder myModule = myAssembly.DefineDynamicModule(assemName.Name, assemName.Name + ".exe");       myAssembly.DefineVersionInfoResource();       myAssembly.Save(assemName.Name + ".exe");    } }