When a assembly is not found, when using the standard probing method, an assembly resolve event is fired, allowing to load the assembly from a different file system path:

 

public void Test()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
}
public static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
{
    string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string assemblyPath = Path.Combine(folderPath, string.Format("{0}.dll", new AssemblyName(args.Name).Name));
    if (!File.Exists(assemblyPath))
    {
        assemblyPath = Path.Combine(folderPath, string.Format("{0}.exe", new AssemblyName(args.Name).Name));
        if (!File.Exists(assemblyPath))
        {
            return null;
        }
    }
    Assembly assembly = Assembly.LoadFrom(assemblyPath);
    return assembly;
}

One Comment

  1. Hi

    I have a plugin based WPF application. Assemblies loaded during XAML parsing does not trigger the AsemblyResolve event.

    Setting the probing path using AppendPrivatePath on CurrentDomain makes it work, but this function is marked as Obsolete.

    Benny Tordrup

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.