代码应创建域,将dll加载到域中,卸载dll
但是在新域上调用卸载后,dll仍然存在吗?
private void Method1()
{
//create new domain
AppDomain domain = AppDomain.CreateDomain("MyDomain");
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
//load dll into new domain
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = "c:\\mycode.dll";
Assembly assembly = domain.Load(assemblyName);
//do work with dll
//...
//unload dll
AppDomain.Unload(domain);
//still showing dll below ?????
Assembly[] aAssemblies = AppDomain.CurrentDomain.GetAssemblies();
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string[] tokens = args.Name.Split(",".ToCharArray());
System.Diagnostics.Debug.WriteLine("Resolving : " + args.Name);
return Assembly.LoadFile(Path.Combine(new string[] { "c:\\", tokens[0] + ".dll" }));
}
为什么dll无法卸载的任何想法?
编辑(具有如下功能)
正如Jean所指出的,domain.Load是问题所在,在同一项目中将CreateInstanceAndUnwrap与代理类一起使用是可行的。)
代替
//load dll into new domain *** this does not work
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = "c:\\mycode.dll";
Assembly assembly = domain.Load(assemblyName);
对此
// loads proxy class into new domain
Type myType = typeof(Proxy);
var value = (Proxy)domain.CreateInstanceAndUnwrap(
myType.Assembly.FullName,
myType.FullName);
//DoWork:- loads assembly into domain
// calls methods, returns result
Int32 result = value.DoWork(pathToDll);
//Unload domain and thus assembly
AppDomain.Unload(domain);
//Assembly not present in current domain
Assembly[] list = AppDomain.CurrentDomain.GetAssemblies();
将下面的类添加到当前名称空间
public class Proxy : MarshalByRefObject
{
public Int32 DoWork(string assemblyPath)
{
try
{
Assembly assembly = Assembly.LoadFile(assemblyPath);
Int32 response = 0;
Type tObject = assembly.GetType("namespace.class");
MethodInfo Method = tObject.GetMethod("method");
Type myType = typeof(Proxy);
object myInstance = Activator.CreateInstance(myType);
response = (Int32) Method.Invoke(myInstance, null);
return response;
}
catch (Exception ex)
{
throw ex;
}
}
}
参考方案
根据经验,一旦有了Assembly
实例,就意味着有问题的程序集已加载到当前域中。
这意味着,当您调用AppDomain.Load
方法时,您既要在当前域中又要在新域中加载程序集。 (正如您在MSDN上对该方法的评论中所看到的,“该方法应仅用于将程序集加载到当前应用程序域中。”)
编辑:以前发布的“解决方案”已被删除,因为它实际上不能解决问题。作者将有效的解决方案编辑为问题。
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
改造正在返回一个空的响应主体 - java我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…
每个文件合并后添加换行 - python我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…
您如何在列表内部调用一个字符串位置? - python我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …