A.exe에서 B.dll을 컴파일시 참조하지 않고  동적으로 참조할 때,

 

B.dll에 있는 특정 클래스 메서드의 파라미터(Delegate)를 넘길때 방법입니다. 

 

리플렉션을 이용해서 대리자 개체를 생성 후 Invoke 파라미터에 넘겨주었습니다.

 

A.exe

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace DelegateReflaction

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            InvokeTest();

        }

 

        private void InvokeTest()

        {

            string dllFilePath = @"D:\Work\Project\Test\DelegateReflaction\TestLibrary\bin\Debug\TestLibrary.dll";

            Assembly assembly = Assembly.LoadFile(dllFilePath);

 

            Type formType = assembly.GetType("TestLibrary.ChiForm");

 

            //Child 델리게이트 타입 가져오기

            Type deleType = assembly.GetTypes().Single(a => a.Name == "CallbackDelegate");

 

 

            //현재 클래스의 Callback 메서드 정보 가져오기

            MethodInfo callBackMethodInfo = this.GetType().GetMethod("Callback", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string) }, null);

 

            //Child CallbackDelegate Delegate를 이용해서 Deleagte 생성

            Delegate callbackMethod = Delegate.CreateDelegate(deleType, this, callBackMethodInfo);

 

            //Child 폼 생성

            Form form = Activator.CreateInstance(formType) as Form;

 

            //Child DoWork 메서드 가져오기

            MethodInfo doworkMethodInfo = formType.GetMethod("DoWork");

 

            //파라미터 생성

            object[] parameters = new object[2];

            parameters[0] = "test~";

            parameters[1] = callbackMethod;

 

            //Child Dowork 메서드 호출

            doworkMethodInfo.Invoke(form, parameters);

 

            form.Show();

        }

 

        void Callback(string msg)

        {

            MessageBox.Show(msg);

        }

    }

}

  

 

 

B.dll

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace TestLibrary

{

    public class ChiForm : Form

    {

        public delegate void CallbackDelegate(string status);

 

        public ChiForm()

        {

            InitializeComponent();

        }

 

        public void DoWork(string param, CallbackDelegate callback)

        {

            callback("status");

        }

 

        private void InitializeComponent()

        {

            this.SuspendLayout();

            //

            // ChiForm

            //

            this.ClientSize = new System.Drawing.Size(284, 262);

            this.Name = "ChiForm";

            this.Text = "ChildForm";

            this.ResumeLayout(false);

 

        }

    }

} 

 

 

프로젝트 (vs2015 작성) :  DelegateReflaction.zip

 

'.Net > C#' 카테고리의 다른 글

C# 실행파일 안에 DLL 넣기  (0) 2016.05.16
C# Undo Redo 기능 구현하기  (3) 2015.10.20
C# 원문자 구하기  (0) 2015.10.05
SerialPort Read Blocking 시키기  (0) 2015.08.05
문자열 비교 테스트 (대소문자 무시)  (0) 2015.07.02

+ Recent posts