我将动态dll添加到我的应用程序中。调用方法后我有内存泄漏。这是我的代码:
static IntfClass GetIClass(string filename)
{
Assembly classLibrary1 = null;
using (FileStream fs = File.Open(filename, FileMode.Open))
{
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[1024];
int read = 0;
while ((read = fs.Read(buffer, 0, 1024)) > 0)
ms.Write(buffer, 0, read);
classLibrary1 = Assembly.Load(ms.ToArray());
}
}
foreach (Type type in classLibrary1.GetExportedTypes())
{
if (type.GetInterface("IntfClass") != null)
return Activator.CreateInstance(type) as IntfClass;
}
throw new Exception("no class found that implements interface IntfClass");
}
主叫:
IntfClass class1 = GetIClass("myDllName.dll");
Thread t = new Thread(new ParameterizedThreadStart(class1.runReport));
t.Start((object)report);
我添加了线程,案例我的应用程序将控制转移到dll,在完成dll方法完成控制后。
答案 0 :(得分:0)
创建一个单独的AppDomain来加载程序集,并尝试使用AppDomain Unload方法卸载它。
例如:
void LoadAssembly(){
AppDomain newDomain = AppDomain.CreateDomain("CreateNewDomain");
newDomain.ExecuteAssembly("filename");
AppDomain.Unload(newDomain);
}