1

我正在动态构建一个 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:

在此处输入图像描述

4

1 回答 1

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

于 2013-11-15T03:37:57.133 回答