动态创建DLL FileVersion不起作用

时间:2013-11-15 03:15:00

标签: c# dll version assemblybuilder

我正在动态构建DLL并且编写FileVersion不起作用。

我正在使用的代码来自此Microsoft链接,我期望EXE / DLL版本为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);
    }

但实际结果是没有编写FileVersion:

enter image description here

1 个答案:

答案 0 :(得分:1)

根据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");

提示:确保其带有3个点的4位数文件版本,例如1.0.4.0。如果我使用1.0.4,则文件版本为0.0.0.0