Load assemblies with dependencies in a different AppDomain

爱⌒轻易说出口 提交于 2019-12-01 21:32:22

This looks like a job for the ResolveEventHandler (more detail on MSDN regarding resolving unknown assemblies)

So, you can write something like

class MyResolver
{
  public static Assembly MyResolveEventHandler( Object sender, ResolveEventArgs args )
  {
    // confirm args.Name contains A.dll
    String dllName = args.Name.Split({','}, SplitStringOptions.None)[0];
    if (dllName == "A")
    {
      return Assembly.LoadFile(@"C:\Root\DirA\A.dll")
    }
    return null;
  }
}

and in the domain you created, you'd do a:

domain.AssemblyResolve += new ResolveEventHandler(MyResolver.MyResolveEventHandler);

Make sure you bind the event before you reference A in B.

AppDomain's cannot probe for dll's outside of their initial folder. They can probe in the GAC, and in the PrivateBinPath deeper into the folder, but they cannot probe into other folders.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!