2012-06-14 1 views
0

개요 :어떻게 매개 변수로 콜백을받는 DLL 메서드를 동적으로 호출합니까?

는 동적으로 .dll을로드하고 자신의 메소드를 호출하는 응용 프로그램을 writting하고있다.

  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"); 

: 배경에, 내가 "거기"무슨 일이 일어나고 있는지에 대한 UI를 통지 코드의

조각을 콜백을했습니다 O를 .dll을 이후

은/중공업을하고있다 여태까지는 그런대로 잘됐다. 문제는 내가이 있지만, 작동하지 않는이 코드는 작동

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(); 

을 (콜백 선언이 작동하면 바로 테스트 할)은 "콜백"메소드를 선언에도 그래도, 그

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 
+0

,하지만 당신은 단지 객체를 전달할 수 있습니다 . 그 이유는 부울을 통과시키는 이유입니다. 이 메서드를 대리자로 사용하고 싶습니까? –

+0

매개 변수로 콜백 메서드를받는 dll에서 메서드를 호출 (호출)하려고합니다. 이것은 기본적으로 내가하려는 일이며, 내가 할 수있는 방법이 있습니까? 방법이없는 경우 전체 앱 아키텍처를 검토해야 할 수도 있습니다. –

+1

첫 번째'crawlMethod.Invoke (...);가'setCallbackMethod.Invoke (...);'가 아니어야합니까? –

답변

2

난 당신이 지나가는이 같은 대리자 형식이 있다고 가정 (이 내가 인수로 콜백을 전달. 해결하기 위해 노력 dinamically .DLL의 메소드를 호출하고있는 무슨이다) SetCallback의 방법 :이 같은이 대리자 형식으로 캐스팅하는 경우

다음
public delegate void CrawlerCallback(string courseName, int subjects, int semesters); 

당신은 Notify 방법을 통과 할 수 있습니다 : 당신은 방법을 전달하는 데 노력하고있다

setCallbackMethod.Invoke(crawlerObj, new object[] { (CrawlerCallback)Notify }); 
+0

와우! 감사합니다. 내 문제가 해결되었습니다. 다시 한 번 감사드립니다. :) –

관련 문제