.net 当前AppDomain正在应用程序库之外加载程序集?

.net 当前AppDomain正在应用程序库之外加载程序集?,.net,assemblies,clr,appdomain,.net,Assemblies,Clr,Appdomain,我正在创建一个新的沙盒AppDomain,其ApplicationBase和PrivateBinPath(例如sake)已设置为C:\MyApp。我正在执行的应用程序正在从C:\somewhere运行 当我otherDomain.Load(…)一个程序集时,我正在执行的AppDomain也在加载该程序集。我通过在加载之前检查GetAssemblies(),然后在加载之后检查GetAssemblies()来确定这一点 为什么会这样?我怀疑这与需要在执行的AppDomain中可用的元数据有关,它是通

我正在创建一个新的沙盒AppDomain,其ApplicationBase和PrivateBinPath(例如sake)已设置为
C:\MyApp
。我正在执行的应用程序正在从
C:\somewhere
运行

当我
otherDomain.Load(…)
一个程序集时,我正在执行的AppDomain也在加载该程序集。我通过在加载之前检查
GetAssemblies()
,然后在加载之后检查
GetAssemblies()
来确定这一点

为什么会这样?我怀疑这与需要在执行的AppDomain中可用的元数据有关,它是通过“跨界”从新域传递回来的,因此调用域也在加载程序集。但是我认为程序集不能在其ApplicationBase之外加载,除非它在GAC中,而在本例中,它不是


有人能帮我解决这个问题吗?

为了不将第二个appdomain的程序集加载到父域中,您不能使用otherdomain.load(…)。您必须在子appDomain中创建MarshallByRefObject,并使该代码调用appDomain.Load(…)

例如:

public class AppDomainInitializer : MarshalByRefObject
{
  public void Initialize() { AppDomain.Load(...); }
}
父域:

{
AppDomain otherDomain = AppDomain.CreateDomain(...);

// Create the object in the other domain
ObjectHandle oh = Activator.CreateInstance(otherDomain, assemblyNme, "AppDomainInitializer", ...);

// Marshall it to this domain
var initializer = (AppDomainInitializer) oh.UnWrap();

// Proxy the call to load up the other domain dll's
intializer.Initialize();    
}