如何在AfterBuild中使用ILMerge和.NET 4.5?

时间:2013-01-03 12:44:06

标签: .net msbuild visual-studio-2012 .net-4.5 ilmerge

我们希望将几个项目的解决方案升级到.NET 4.5。我们已经使用Visual Studio 2012.我们使用ILMerge将程序集合并到单个EXE。

我们当前的主项目.csproj文件如下所示:

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
    <CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
        <Output ItemName="AssembliesToMerge" TaskParameter="Include" />
    </CreateItem>
    <PropertyGroup>
        <ReferenceAssemblies>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0</ReferenceAssemblies>
    </PropertyGroup>
    <Message Importance="high" Text="Executing ILMerge...with target platform from $(ReferenceAssemblies)" />
    <Exec Command="&quot;$(SolutionDir)LIB\ILMerge.exe&quot; /out:@(MainAssembly) /internalize /targetplatform:v4,&quot;$(ReferenceAssemblies)&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" />
    <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>

这应该如何看待.NET 4.5?

我已阅读here使用ILMerge和.NET 4.5存在一些问题。

1 个答案:

答案 0 :(得分:12)

我找不到任何关于此的好文档,但正如Matt Wrocks blog postanother question about ILMerge所建议的那样,我首先尝试使用与.NET 4相同的参考组件路径。

在将我们的NuGet包重新定位到.NET 4.5之前,这似乎首先起作用。 (特别是Microsoft.AspNet.WebApi.Client,它添加了对新的.NET 4.5程序集System.Net.Http.WebRequest的引用,之前它包含在NuGet包中。)

更新参考程序集到.NET 4.5的路径后,它工作正常:

C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.5

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
    <CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
        <Output ItemName="AssembliesToMerge" TaskParameter="Include" />
    </CreateItem>
    <PropertyGroup>
        <ReferenceAssemblies>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5</ReferenceAssemblies>
    </PropertyGroup>
    <Message Importance="high" Text="Executing ILMerge...with target platform from $(ReferenceAssemblies)" />
    <Exec Command="&quot;$(SolutionDir)LIB\ILMerge.exe&quot; /out:@(MainAssembly) /internalize /targetplatform:v4,&quot;$(ReferenceAssemblies)&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" />
    <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>

在大多数情况下,.NET 4程序集的路径也可以工作,但在.NET 4.5中引用新程序集时,需要更新路径。

请注意,在示例中,ILMerge.exe被下载到解决方案目录中名为LIB的文件夹中。