问题
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.
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);
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.
回答1:
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>
At least, every time I do a Rebuild it appears to generate an assembly with a different GUID for a name.
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/
来源:https://stackoverflow.com/questions/30089576/unique-assembly-name-every-build