我試圖加載第三方assemblies
動態到項目和使用reflection
來創建它們的類型的實例。如何動態地將程序集加載到當前應用程序域到c#項目中?
我用:
Assembly.LoadFrom("Assembly1.dll")
Assembly.LoadFrom("Assembly2.dll")
Assembly.LoadFrom("Assembly3.dll")
此外,嘗試:
AppDomain.CurrentDomain.Load("Assembly1.dll")
AppDomain.CurrentDomain.Load("Assembly2.dll")
AppDomain.CurrentDomain.Load("Assembly3.dll")
不過,我不斷收到The method is not implemented
例外,當我嘗試創建自己的類型如下的一個實例:
Assembly.LoadFrom("Assembly1.dll")
Assembly.LoadFrom("Assembly2.dll")
Assembly assembly= Assembly.LoadFrom("Assembly3.dll")
Type type=assembly.GetType("Assembly3.Class1")
object instance=Activator.CreateInstance(type); //throws exception at this point
但是,如果我直接add reference
到作爲sembly1,Assembly2和Assembly3項目做:
Assembly3.Class1 testClass=new Assembly3.Class1();
我沒有得到任何例外
我只是想知道我做錯了嗎?如何動態地將組件加載到項目。我猜是因爲創建Class1
實例取決於另一個程序集Assembly1
和Assembly2
,所以它失敗了。那麼,如何將所有相關程序集動態加載到appdomain/loadcontext
。
非常感謝您的回答。
在另一個線程中回覆:http://stackoverflow.com/a/18362459/4997569 – ArcangelZith
不是真的!只加載_one assembly_並實例化它的'type'。我已經嘗試過了。但沒有奏效。我**多個程序集**加載到「當前上下文」並創建一個實例 –