.net Reflection event registration

Published Sep 27, 2022 | Updated Mar 8, 2026 | 0 comments

리플렉션으로 이벤트를 등록하는 샘플

- 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

TAG INFORMATION

Learn more on this topic

Related Posts

무료 온라인 전광판

전광판

텍스트를 입력하고 텍스트 효과 및 배경효과 를 변경해서 전체화면으로 표시할 수 있는 전광판 용도로 사용하실 수 있습니다. 각종 스포츠 및 공연 관람시 응원 용도로 사용이 가능합니다.

Carousel

여러개의 슬라이드를 추가하여 프레젠테이션 및 이미지 슬라이드 용도로 사용하실 수 있습니다. 브라우저가 포함된 IT 기기로 큰 모니터에 연결하여 매장 내 공지사항 및 메뉴소개를 이미지로 표시할 수 있습니다.

Pin It on Pinterest

Shares
Share This

Share This

Share this post with your friends!

Shares