I have a console application which is referencing other projects in solution. When I build it, it will copy those dlls in Debug. I want to import them in the exe. If I add them to resources then load from there, they are not updated. I lose the changes on referenced DLLs. Is there a way that I can build them and import them in the executable file on each build?
问题:
回答1:
You can use ILMerge to merge several assemblies into one.
回答2:
Jeffrey Richter has an article on this very topic:
http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
The key is
For each DLL file you add, display its properties and change its Build Action to Embedded Resource.
回答3:
Here's the solution that worked for me:
http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx
It merges assemblies after each build with ILMerge (like suggested in comments). I needed to update .targets file for .NET Framework 4. In case anyone needs it:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<CreateItem Include="@(ReferencePath)" Condition="'%(CopyLocal)'=='true' and '%(ReferencePath.IlMerge)'=='true'">
<Output TaskParameter="Include" ItemName="IlmergeAssemblies"/>
</CreateItem>
<Message Text="MERGING: @(IlmergeAssemblies->'%(Filename)')" Importance="High" />
<Exec Command=""$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe" /out:@(MainAssembly) "@(IntermediateAssembly)" @(IlmergeAssemblies->'"%(FullPath)"', ' ') /target:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /wildcards" />
</Target>
<Target Name="_CopyFilesMarkedCopyLocal"/>
</Project>
Update
While the solution above works, you can make it simpler and you wouldn't need the targets files. You can put ILMerge somewhere in solution. Then call it from there after build. ILMerge.exe is all you need, copy it in somewhere like /solutionDirectory/Tools
. Write a command in your post-build event command line.
$(SolutionDir)Tools\ILMerge.exe /out:"$(ProjectDir)bin\Debug\WindowsGUI.exe" "$(ProjectDir)obj\x86\Debug\WindowsGUI.exe" "$(SolutionDir)BusinessLayer\bin\Debug\BusinessLayer.dll" /target:exe /targetplatform:v4,"$(MSBuildBinPath)" /wildcards
After the build, you get the .exe with embedded DLLs and you can run it alone.