2009-02-05 24 views
0

給定一個.NET DLL,它由一個類「Place」和一個返回整數的函數「Where」組成;我需要將DLL加載到應用程序域中,執行該功能並卸載應用程序域。執行應用程序域中的函數


Dim domain As AppDomain = AppDomain.CreateDomain("Executor")    
Dim buffer() As Byte = IO.File.ReadAllBytes("c:\path\Locator.dll") 
Dim asy As Assembly = domain.Load(buffer) 
Dim obj As [Object] = asy.CreateInstance("Locator.Place") 
Dim method As MethodInfo = obj.GetType.GetMethod("Where") 
Dim result as Integer = method.Invoke(obj, New [Object]() { 1 }) 
AppDomain.Unload(domain) 

此行失敗:


Dim asy As Assembly = domain.Load(buffer) 

與此錯誤消息:


'Could not load file or assembly 'Place, Version=1.0.0.0, Culture=neutral, PublicKeyToken-null' or one of it's dependencies. The System Cannot find the specified file.' 

的文件是在緩衝區中,所以我猜這是一個.dll文件扶養。但是,它應該在基本程序目錄中找到它們。

關於錯誤原因的任何想法?

任何測試的示例代碼用於將程序集加載到AppDomain中,執行函數,然後卸載AppDomain,將不勝感激。

(順便說一句,我用Google搜索,並沒有發現任何有用的樣品

回答

1

您在說明錯誤是由於缺少參考時是正確的。由於您如何加載程序集,最有可能無法解析該引用。由於您正在從一個字節數組中加載,Assembly.Location不會指向該dll的位置。由於您引用的dll不在GAC中,因此將無法找到引用的程序集。嘗試直接從文件加載程序集,而不是首先加載到字節數組中。