잠토의 잠망경

C#에서 Dll 동적 로딩하기 본문

공부/C sharp

C#에서 Dll 동적 로딩하기

잠수함토끼 2013. 12. 7. 19:55

C#에서 Dll 동적 로딩하기

밑에서 참고 하여 발전 개선 진행함

http://whiteberry.egloos.com/viewer/1953059

 

나는 이중에서 delegate 방식이 가장 좋았던 것 같다.

 

Dll 부분

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace TestDll

{

    public class Class1

    {

 

        public void swap(ref int num1, ref int num2)

        {

            int temp = num1;

            num1 = num2;

            num2 = temp;

        }

    }

}

 

 

Dll 이용하는 Code – 1 delegate 이용

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

using System.Reflection;

 

 

namespace TestAppUsingDll

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        delegate void TheMethodDelegate(ref int a, ref int b);

 

        private void button1_Click(object sender, EventArgs e)

        {

            //http://whiteberry.egloos.com/viewer/1953059

 

            Assembly u = Assembly.LoadFile(@"C:\Users\Administrator\Documents\Visual Studio 2013\Projects\TestDll\TestDll\bin\Debug\TestDll.dll");

 

            Module[] modules = u.GetModules();

 

            Type t = modules[0].GetType("TestDll.Class1");

 

            MethodInfo minfo = t.GetMethod("swap");

 

            //http://msdn.microsoft.com/ko-kr/library/53cz7sc6(v=vs.110).aspx

 

            TheMethodDelegate testMethod = (TheMethodDelegate)Delegate.CreateDelegate(typeof(TheMethodDelegate), minfo);

 

 

            int a = 10;

            int b = 11;

 

            testMethod(ref a, ref b);

 

 

        }

    }

}

 

 

Dll 이용하는 Code – 2Invoke 이용

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

using System.Reflection;

 

 

namespace TestAppUsingDll

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        delegate void TheMethodDelegate(ref int a, ref int b);

 

        private void button1_Click(object sender, EventArgs e)

        {

            Assembly u = Assembly.LoadFile(@"C:\Users\Administrator\Documents\Visual Studio 2013\Projects\TestDll\TestDll\bin\Debug\TestDll.dll");

 

            Module[] modules = u.GetModules();

 

            Type t = modules[0].GetType("TestDll.Class1");

 

            MethodInfo minfo = t.GetMethod("swap");

           

 

            object[] parameter = { 10, 11 };

 

            minfo.Invoke(minfo, parameter);

       

        }

    }

}

 

 

Dll 이용하는 Code – 3 Invoke 이용

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

using System.Reflection;

 

 

namespace TestAppUsingDll

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        delegate void TheMethodDelegate(ref int a, ref int b);

 

        private void button1_Click(object sender, EventArgs e)

        {

            Assembly u = Assembly.LoadFile(@"C:\Users\Administrator\Documents\Visual Studio 2013\Projects\TestDll\TestDll\bin\Debug\TestDll.dll");

 

            object instanceObject = u.CreateInstance("TestDll.Class1");

 

            object[] parameter = {10, 11};

 

            instanceObject.GetType().GetMethod("swap").Invoke(instanceObject,parameter);

 

        }

    }

}

 

Comments