Search code examples
c#dllversionassemblybuilder

Creating a DLL dynamically the FileVersion doesn't work


I am building a DLL Dynamically and writing the FileVersion doesn't work.

The code I'm using is from this Microsoft link and I am expecting the EXE/DLL version to be 1.0.0.2001: http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.version.aspx

    public static void Main()
    {
        // Create a dynamic assembly with name 'MyAssembly' and build version '1.0.0.2001'.
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.Name = "MyAssembly";
        myAssemblyName.Version = new Version("1.0.0.2001");
        MakeAssembly(myAssemblyName, "MyAssembly.exe");
    }

    public static void MakeAssembly(AssemblyName myAssemblyName, string fileName)
    {
        // Get the assembly builder from the application domain associated with the current thread.
        AssemblyBuilder myAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
        // Create a dynamic module in the assembly.
        ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", fileName);
        // Create a type in the module.
        TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
        // Create a method called 'Main'.
        MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.HideBySig |
           MethodAttributes.Static, typeof(void), null);
        // Get the Intermediate Language generator for the method.
        ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
        // Use the utility method to generate the IL instructions that print a string to the console.
        myILGenerator.EmitWriteLine("Hello World!");
        // Generate the 'ret' IL instruction.
        myILGenerator.Emit(OpCodes.Ret);
        // End the creation of the type.
        myTypeBuilder.CreateType();
        // Set the method with name 'Main' as the entry point in the assembly.
        myAssemblyBuilder.SetEntryPoint(myMethodBuilder);
        myAssemblyBuilder.Save(fileName);
    }

But the actual results are no FileVersion is written:

enter image description here


Solution

  • As per AssemblyBuilder.DefineVersionInfoResource

    Type attributeType = typeof(AssemblyFileVersionAttribute);
    
    // To identify the constructor, use an array of types representing 
    // the constructor's parameter types. This ctor takes a string. 
    //
    Type[] ctorParameters = { typeof(string) };
    
    // Get the constructor for the attribute. 
    //
    ConstructorInfo ctor = attributeType.GetConstructor(ctorParameters);
    
    // Pass the constructor and an array of arguments (in this case, 
    // an array containing a single string) to the  
    // CustomAttributeBuilder constructor. 
    // 
    object[] ctorArgs = { "1.0.4.0" };     
    CustomAttributeBuilder attribute =
       new CustomAttributeBuilder(ctor, ctorArgs);
    
    // Finally, apply the attribute to the assembly. 
    //
    assemblyBuilder.SetCustomAttribute(attribute);
    assemblyBuilder.DefineVersionInfoResource();
    
    // Finally, save the assembly
    assemblyBuilder.Save(name.Name + ".dll");
    

    Tip: make sure its a 4 digit File Version with 3 dots, eg 1.0.4.0. If I use 1.0.4 the file version comes out as 0.0.0.0