아래 샘플을 참조하십시오. 나는 사건에 반사 되더라도 얻은 DoSomething 메서드를 연결해야합니다.리플렉션을 사용하여 이벤트를 연결하는 방법
class Program {
private static event EventHandler MyEvent;
static void Main(string[] args)
{
object aType = new SomeType();
var type = aType.GetType();
var method = type.GetMethod("DoSomething");
if (method != null)
{
MyEvent += method;//How do I wire this up?
}
}
}
public class SomeType {
public void DoSomething() {
Debug.WriteLine("DoSomething ran.");
}
}
당신은 또한 빠른 응답을위한 이벤트 핸들러에 –
감사 CreateDelegate의 결과를 캐스팅해야합니다. 이것은 매우 도움이되었습니다. –