這是可能的,如果不是這樣可能有人請解釋我可以如何實現這個工作?C#從DLL擴展主機應用程序類型創建類型的實例
所以我有一個C#應用程序,並用它的一個文件夾稱爲模塊,所以文件:
Application.exe
modules/
Application.Handler.ModuleName.dll
所以該DLL內它的命名空間Application.Handle
包含類型ModuleName
和ModuleName
延伸Handler
了在應用實施.exe所以編譯需要Application.exe
作爲參考。
在我的主機應用程序,我有:
string[] dirs = Directory.GetFiles(@"modules/", "Application.Handler.*.dll");
foreach(string filePath in dirs)
{
Assembly.LoadFile(new FileInfo(filePath).FullName);
string fileName = filePath.Split('/').Last();
string typeAssemblyName = fileName.Replace(".dll", "");
string typeName = typeAssemblyName.Split('.').Last();
}
但我不確定如果我能實現從字符串類型我以爲我可以用Activator.CreateInstance
,但我不知道如果我這樣做正確或如果我試圖實現它的方式工作?
UPDATE 我可能沒有明確的,但有效的什麼,我需要做的是 Application.Handler handler = new Application.Handler.ModuleName()
凡Application.Handler.ModuleName
在PHP這就像下面我以爲會有一個返回字符串中給定類型的對象的系統來完成。如果它不存在拋出異常
$className = "\Application\Handler\ModuleName";
$instance = new $className();
我也使用@rene使用這種方法在這裏我給它的Assembly
名字它給了我一個FileNotFoundException
並沒有建議
Assembly asm = Assembly.LoadFile(new FileInfo(filePath).FullName);
string fileName = filePath.Split('/').Last();
string typeAssemblyName = fileName.Replace(".dll", "");
string typeName = typeAssemblyName.Split('.').Last();
FrameHandler fh;
fh = (FrameHandler)Activator.CreateInstance(asm.FullName, typeAssemblyName).Unwrap();
fh.RegisterHandlers();
的Unwrap
系統試圖Assembly
名稱我得到TypeLoadException
但它必須加載程序集的清單Application.Handler.ModuleName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
不能使用[MEF(http://stackoverflow.com/questions/19572647/using-mef-with-c-how-do-i-call-methods-on-the-host - 從最插件)? – rene
我所得到的只是'類型或namepsace'組合'不存在於......' –
我至少要確保共享的*接口*在應用程序引用的單獨程序集中聲明,而「插件「DLL。通過這樣做,你將避免循環依賴/ refs。 – spender