Created
April 17, 2013 18:01
-
-
Save derekgates/5406412 to your computer and use it in GitHub Desktop.
Binding assembly loading to assemblies packed in resources files. Does NOT work with strong named assemblies due to .NET security!
This file contains hidden or 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
[STAThread] | |
static int Main(string[] args) | |
{ | |
//load assembly via the resources (such as using Costura advanced): | |
AttachResolverForLocalPaths(); | |
} | |
public static void AttachResolverForLocalPaths() | |
{ | |
var currentDomain = AppDomain.CurrentDomain; | |
currentDomain.AssemblyResolve += OnCurrentDomainOnAssemblyResolve; | |
} | |
public static Assembly OnCurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) | |
{ | |
var name = new AssemblyName(args.Name).Name; | |
var assemblyResourceName = string.Format("{0}.dll", name); | |
var executingAssembly = Assembly.GetExecutingAssembly(); | |
string dir = Path.GetDirectoryName(executingAssembly.Location); | |
if (!File.Exists(Path.Combine(dir, assemblyResourceName))) | |
return null; | |
return Assembly.LoadFile(Path.Combine(dir, assemblyResourceName)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment