2011-06-20 26 views
0

上午使用下面的代碼來創建新的AppDomain並加載我的程序集,其中我的程序集加載成功,但在我調用AppDomain.Unload方法後,仍然有鎖加載的DLL,我去創建新的AppDomain解決方案,因爲我發現在網絡上,這是推薦的技術來加載/卸載程序集,也使程序集可替換,任何想法?AppDomain.Unload不會刪除DLL上的鎖

這裏是我的代碼:

public sealed class AssemblyLoader : MarshalByRefObject 
{ 
    private static AppDomain LocalDomain = AppDomain.CreateDomain("LocalDomain"); 

    public static Assembly LoadAssembly(string path) 
    { 
     try 
     { 
      Assembly assembly = Assembly.Load(AssemblyName.GetAssemblyName(path)); 
      if (assembly != null) 
       return assembly; 
     } 
     catch { } 

     return Assembly.LoadFrom(path); 
    } 

    public static void Unload() 
    { 
     if (LocalDomain != null) 
     { 
      AppDomain.Unload(LocalDomain); 
      LocalDomain = null; 
     } 
    } 
} 

回答

0

我不知道,但是從理論上它不應該是更喜歡這一點。 您不是通過新創建的AppDomain加載程序集。

public static Assembly LoadAssembly(string path) 
{ 
    try 
    { 
     Assembly assembly = LocalDomain.Load(AssemblyName.GetAssemblyName(path)); 
     if (assembly != null) 
      return assembly; 
    } 
    catch { } 

    // .. 
} 
+0

我想你的代碼中現在面臨這樣的錯誤: 無法加載文件或程序集「MyAssembly程序,版本= 1.0.0.0,文化=中性公鑰= null或它的一個依賴。該系統找不到指定的文件。 – akkad

+0

我想在這裏提到,我的程序集駐留在另一個目錄比斌。 – akkad

+0

您需要訂閱「AssemblyResolve」事件。 http://msdn.microsoft.com/en-US/library/system.appdomain.assemblyresolve.aspx –