리플렉션으로 이벤트를 등록하는 샘플
- MDI 자식폼에서 이벤트 등록 및 호출(C#)
public delegate void TestDelegate(object sender, string str); public event TestDelegate OnTested; private void btnRefer_Click(object sender, EventArgs e) { if(OnTested != null) { OnTested(this, "test"); } }
- 메인 폼에서 리플렉션으로 해당 폼 생성시 이벤트 등록(VB.NET)
oUserControl = CType(oAsm.CreateInstance(aAssemblyNm & "."& aClassNm), UserControl) If (aAssemblyNm = "KKOMZI.Test" And aClassNm = "TST001") Then Dim eventInfo As EventInfo = oUserControl.GetType().GetEvent("OnTested") Dim type As Type = eventInfo.EventHandlerType Dim method As MethodInfo = Me.GetType().GetMethod("TestShow", BindingFlags.NonPublic Or BindingFlags.Instance) Dim handler As [Delegate] = [Delegate].CreateDelegate(type, Me, method) eventInfo.AddEventHandler(oUserControl, handler) End If
- 메인 폼에서 이벤트 발생시 실행될 메서드(VB.NET)
Private Sub TestShow(ByVal sender As Object, ByVal str As String) MessageBox.Show(str) End Sub