我已经在How to load a .NET assembly for reflection operations and subsequently unload it?
中为第4个答案实现了代码模拟当我尝试加载两个程序集,一个调试和另一个版本时,我收到一个FileLoadException,并显示消息“无法从同一appdomain中的新位置加载”
我正在使用的代码是:
private String rootAssembly;
private void button1_Click(object sender, EventArgs e)
{
rootAssembly = "{...path to release version...}\TestApp.exe";
TempLoadAssembly();
}
private void button2_Click(object sender, EventArgs e)
{
rootAssembly = "{...path to debug version...}\TestApp.exe";
TempLoadAssembly();
}
public void TempLoadAssembly()
{
AppDomain tempDomain = AppDomain.CreateDomain("TemporaryAppDomain");
tempDomain.DoCallBack(LoaderCallback);
AppDomain.Unload(tempDomain);
}
private void LoaderCallback()
{
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(rootAssembly);
IList<CustomAttributeData> customAttributeDatas = CustomAttributeData.GetCustomAttributes(assembly);
// do other work here
}
我的代码中是否有错误,或者是否无法加载两个版本的程序集?我在新的临时域上调用AppDomain.Unload()来回避这个问题,但它似乎没有用。