0
我正在寫一個應用程序來動態加載.dlls並調用它們的方法。
由於.DLL文件都在做大量的I/O的背景下,我做了回調以通知發生了什麼UI 「那裏」 代碼的
件:
dllName = (string) e.Argument;
// Assembling Complete path for the .dll file
completePath = Path.Combine(ConfigurationManager.AppSettings["DllsFolder"], dllName);
Assembly assembler = Assembly.LoadFrom (completePath);
// Creating Instance of Crawler Object (Dynamically)
dllWithoutExtension = Path.GetFileNameWithoutExtension (dllName);
Type crawlerType = assembler.GetType (dllWithoutExtension + ".Crawler");
object crawlerObj = assembler.CreateInstance (crawlerType.FullName);
// Fetching reference to the methods that must be invoked
MethodInfo crawlMethod = crawlerType.GetMethod ("StartCrawling");
MethodInfo setCallbackMethod = crawlerType.GetMethod ("SetCallback");
到現在爲止還挺好。 的問題是,即使壽我已經宣佈了「回調」方法
public void Notify (string courseName, int subjects, int semesters)
{
string course = courseName;
int a = subjects;
int b = semesters;
}
此代碼的工作雖然這,不工作(只是爲了測試,如果回調申報工作)
Crawler crawler = new Crawler();
crawler.SetCallback (Notify);
crawler.StartCrawling();
(這是我試圖修復。dinamically調用該.dll方法,將回調作爲參數)
setCallbackMethod.Invoke(crawlerObj, new object[] { Notify }); // this method fails, bc its a callback parameter
crawlMethod.Invoke(crawlerObj, new object[] {true} ); // This method works, bc its a bool parameter
你試圖傳遞的方法,但你可以只傳遞對象。這就是傳遞布爾值的原因。你可能想使用該方法作爲委託? –
我想從一個dll調用(調用)一個方法,它接收一個回調方法作爲參數。 這基本上是我想要做的,有什麼辦法可以做到嗎? 如果沒有辦法,我可能不得不查看整個應用程序結構。 –
不應該第一個'crawlMethod.Invoke(...);'是'setCallbackMethod.Invoke(...);'? –