Embed referenced assemblies, because ILMerge won't work with WPF applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class App : Application { | |
public App() { | |
AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly; | |
} | |
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args) { | |
var executingAssembly = Assembly.GetExecutingAssembly(); | |
var assemblyName = new AssemblyName(args.Name); | |
var path = assemblyName.Name + ".dll"; | |
if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false) { | |
path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path); | |
} | |
using (var stream = executingAssembly.GetManifestResourceStream(path)) { | |
if (stream == null) | |
return null; | |
var assemblyRawBytes = new byte[stream.Length]; | |
stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length); | |
return Assembly.Load(assemblyRawBytes); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<!-- | |
Originally written by Daniel Chambers (http://www.digitallycreated.net) | |
http://www.digitallycreated.net/Blog/61/combining-multiple-assemblies-into-a-single-exe-for-a-wpf-application | |
--> | |
<Target Name="EmbedReferencedAssemblies" AfterTargets="ResolveAssemblyReferences"> | |
<ItemGroup> | |
<!-- get list of assemblies marked as CopyToLocal --> | |
<AssembliesToEmbed Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)' == '.dll'"/> | |
<!-- add these assemblies to the list of embedded resources --> | |
<EmbeddedResource Include="@(AssembliesToEmbed)"> | |
<LogicalName>%(Filename)%(Extension)</LogicalName> | |
</EmbeddedResource> | |
<!-- no need to copy the assmblies locally anymore --> | |
<ReferenceCopyLocalPaths Remove="@(AssembliesToEmbed)"/> | |
</ItemGroup> | |
<Message Importance="high" Text="Embedding: @(AssembliesToEmbed->'%(Filename)%(Extension)', ', ')"/> | |
</Target> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I edited EmbeddedResource action a bit. So it will properly handle localized dlls:
You have all the necessary code in
ResolveAssembly
already, so it does not need any fixes.