2016-08-06 17 views
1

현재 후기 바인딩 장에서 배우고있는 C#을 배우고 있습니다. 테스트를 위해 다음을 작성했지만 MissingMethodException을 생성합니다. 사용자 지정 개인 DLL을로드하고 성공적으로 메서드를 호출 한 다음 GAC DLL과 동일한 작업을 시도했지만 실패했습니다.Late binding MissingMethodException

나는 다음과 같은 코드로 잘못 알고하지 않습니다

//Load the assembly 
Assembly dll = Assembly.Load(@"System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "); 

//Get the MessageBox type 
Type msBox = dll.GetType("System.Windows.Forms.MessageBox"); 

//Make an instance of it 
object msb = Activator.CreateInstance(msBox); 

//Finally invoke the Show method 
msBox.GetMethod("Show").Invoke(msb, new object[] { "Hi", "Message" }); 
+0

:

MessageBox.Show("Hi", "Message"); 

반사를 통해 정적 메서드를 호출하려면,이 같은 Invoke 방법에 첫 번째 매개 변수로 null를 전달할 수 있습니다 'MessageBox' 클래스는 public 생성자를 가지고 있지 않으며, 정적 메소드를 통해 사용되기로되어 있습니다. –

답변

2

당신은이 라인에 MissingMethodException을 얻고있다 :

object msb = Activator.CreateInstance(msBox); 

MessageBox 클래스에는 public 생성자가 없기 때문입니다. 이 클래스는 다음과 같이 정적 방법을 통해 사용하도록되어 :

//Load the assembly 
Assembly dll = 
    Assembly.Load(
     @"System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "); 

//Get the MessageBox type 
Type msBox = dll.GetType("System.Windows.Forms.MessageBox"); 

//Finally invoke the Show method 
msBox 
    .GetMethod(
     "Show", 
     //We need to find the method that takes two string parameters 
     new [] {typeof(string), typeof(string)}) 
    .Invoke(
     null, //For static methods 
     new object[] { "Hi", "Message" });