每个版本的唯一程序集名称 [英] Unique Assembly name every build
问题描述
我正在尝试在构建过程中更改dll的程序集名称.我创建了另一个exe文件,用于更改csproj文件中的程序集名称,该文件是在预构建过程中执行的.但是似乎我对csproj文件的更改仅在构建完成后才生效,这当然不是我想要的.
I’m trying to change the Assembly name of my dll during build. I’ve created another exe that changes the assembly name in the csproj file, which I execute during pre-build. But it seems like my changes to the csproj file only take effect after the build has finished, which is of course not what I want.
程序集名称每次生成时都必须是唯一的,因此我通过附加GUID来创建它.
The assembly name has to be unique every time it gets built, so I create it by appending a GUID.
var xmlDoc = XDocument.Load(projectFile);
XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
string newAssemblyName = originalAssemblyName + "_" + Guid.NewGuid().ToString("N");
xmlDoc.Element(ns + "Project").Element(ns + "PropertyGroup").Element(ns + "AssemblyName").Value = newAssemblyName;
xmlDoc.Save(projectFile);
我想知道是否可以在预构建过程中强制"重新加载csproj,或者是否有另一种方法可以在每次构建解决方案时获得唯一的程序集名称.
I was wondering if there is maybe a way to ‘force’ reload the csproj during pre-build or if there is another way I could get a unique assembly name everytime I build my solution.
推荐答案
在VS2013的csproj文件中,我做到了一些成功:
I had some success doing this inside the csproj file in VS2013:
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
[...]
<PropertyGroup>
<MyNewGuid>$([System.Guid]::NewGuid())</MyNewGuid>
</PropertyGroup>
[...]
<AssemblyName>$(MyNewGuid)</AssemblyName>
至少,每次执行重建时,它似乎都会生成一个具有不同GUID名称的程序集.
At least, every time I do a Rebuild it appears to generate an assembly with a different GUID for a name.
如果您使用的是特别旧版本的msbuild,则可能需要在此处查找创建GUID的另一种方法:
If you are using an especially old version of msbuild, you might want to look here for an alternative way to create GUIDs: http://phoebix.com/2013/08/08/who-got-the-func-part-3-generating-a-guid-in-msbuild/
这篇关于每个版本的唯一程序集名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!