2013-11-15 57 views
1

我正在構建動態DLL,並且寫入FileVersion不起作用。動態創建DLL文件版本不起作用

的代碼我使用的是從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); 
    } 

但實際的結果是沒有文件版本寫的是:

enter image description here

+1

檢查** AssemblyBuilder.DefineVersionInfoResource **的MSDN文檔(http://msdn.microsoft.com/zh-cn/library/14dk10z1。 aspx)那裏的例子部分可能有你正在尋找的代碼。但是,我不是100%確定的,因爲我沒有測試過自己(不要混淆文檔是針對看似無關的屬性的,這個例子很重要。) – elgonzo

+1

@elgonzo非常感謝你。提示在[so]註釋中使用超鏈接的[Name](http://)語法。 –

+0

感謝您的超鏈接提示:) – elgonzo

回答

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

提示:確保其4位數的文件版本帶有3個點,例如1.0.4.0。如果我使用1.0.4,則文件版本爲0.0.0.0