更改编译的程序集版本信息 [英] change compiled assembly version information

查看:24
本文介绍了更改编译的程序集版本信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 exe 和一堆 dll,但它们的命名并不强.

I have an exe and a bunch of dlls and they are not in strong naming.

exe 需要具有不同版本的 dll 之一的特定版本,因此在启动 exe 时会发生错误.

The exe requires specific version of one of the dlls which has a different version so an error occurs when launching the exe.

我没有此 dll 的源代码来更改其程序集版本,因此是否可以在外部更改此 dll 的版本或其在 exe 中的引用?

I don't have the source code for this dll to change its assembly version so is it possible to change this dll's version or its reference within the exe externally?

我曾尝试使用 dll 使用ILMerge.exe Foo.dll/ver:1.2.3.4/out:Foo2.dll",但生成的 dll 版本保持不变.

I'd tried "ILMerge.exe Foo.dll /ver:1.2.3.4 /out:Foo2.dll" with the dll, but the resulting dll's version remains the same.

有什么想法吗?

谢谢,

推荐答案

您可以使用 Mono.Cecil (https://www.nuget.org/packages/Mono.Cecil/)使用 Mono.Cecil 打开程序集并查找 AssemblyFileVersionAttribute 并进行必要的更改,然后保存程序集 (dll) 并使用修改后的文件.

You can do this easily using Mono.Cecil (https://www.nuget.org/packages/Mono.Cecil/) Open the assembly using Mono.Cecil and look for the AssemblyFileVersionAttribute and make the necessary changes and then save the assembly (dll) and use the modified file.

为了更新 exe,您需要执行一些非常类似于更新依赖(程序集)dll 版本号的操作.

In order to update the exe, you'd do something very similar to update the dependent (assembly) dll version number.

见下文(更新以包含代码示例以更改程序集 (dll) 的版本以及 exe 中的元数据):

See below (updated to include the code sample to change the version of the assembly (dll) as well as the metadata in the exe):

void Main(){
    UpdateDll();
    UpdateExe();
}
static void UpdateExe(){
    var exe = @"C:	empsample.exe";
    AssemblyDefinition ass = AssemblyDefinition.ReadAssembly(exe);
    var module = ass.Modules.First();
    var modAssVersion = module.AssemblyReferences.First(ar => ar.Name == "ClassLibrary1");
    module.AssemblyReferences.Remove(modAssVersion);
    modAssVersion.Version = new Version(4,0,3,0);
    module.AssemblyReferences.Add(modAssVersion);
    ass.Write(exe);
}
static void UpdateDll()
{
    String assemblyFile = @"C:	empassemblyName.dll";
    AssemblyDefinition modifiedAss = AssemblyDefinition.ReadAssembly(assemblyFile);

    var fileVersionAttrib = modifiedAss.CustomAttributes.First(ca => ca.AttributeType.Name == "AssemblyFileVersionAttribute");
    var constArg = fileVersionAttrib.ConstructorArguments.First();
    constArg.Value.Dump();
    fileVersionAttrib.ConstructorArguments.RemoveAt(0);
    fileVersionAttrib.ConstructorArguments.Add(new CustomAttributeArgument(modifiedAss.MainModule.Import(typeof(String)), "4.0.3.0"));

    modifiedAss.Name.Version = new Version(4,0,3,0);
    modifiedAss.Write(assemblyFile);
}

这篇关于更改编译的程序集版本信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆