Assembly를 통해 DLL 동적 로드

2861 단어 assembly
    Assembly     DLL
      // DLL       
        static Assembly amy = Assembly.LoadFrom("Student.dll");

        //    
        static Type type = amy.GetType("Student.Stu");
        private void button1_Click(object sender, EventArgs e)
        {
            //    
            string strDyNamic= (string)type.InvokeMember("GetNewValue",
                BindingFlags.DeclaredOnly |
                BindingFlags.Public | BindingFlags.Static |
                BindingFlags.InvokeMethod, null, null, 
                new object[] {"Static"});
            MessageBox.Show(strDyNamic);

            //     
            object obj = type.InvokeMember(null,BindingFlags.DeclaredOnly|
                BindingFlags.Public|BindingFlags.NonPublic|
                BindingFlags.Instance|BindingFlags.CreateInstance,
                null,null,null);

            string strStatic = (string)type.InvokeMember("GetValue",
                BindingFlags.DeclaredOnly|
                BindingFlags.Public|BindingFlags.NonPublic|
                BindingFlags.Instance|BindingFlags.InvokeMethod,
                null,obj,new object[]{"NonStatic"});
            MessageBox.Show(strStatic);

            //      
            type.InvokeMember("Name",BindingFlags.DeclaredOnly|
                BindingFlags.Public |BindingFlags.NonPublic|
                BindingFlags.Instance|BindingFlags.SetProperty,
                null,obj,new object[]{"Test"});

            //      
            string property = (string)type.InvokeMember("Name",BindingFlags.DeclaredOnly|
                BindingFlags.Public|BindingFlags.NonPublic|
                BindingFlags.Instance|BindingFlags.GetProperty,
                null,obj,null);
            MessageBox.Show(property);
        }

DLL 코드는 대체로 다음과 같습니다.
namespace Student
{
    public class Stu
    {
       
        public Stu()
        {
            strName = "";
        }
        public string GetValue(string strText)
        {
            return string.Format("Print NonStatic {0}!", strText);
        }
        public static string GetNewValue(string strText)
        {
            return string.Format("Print static {0}!", strText);
        }
        private string strName;
        public string Name
        {
            get { return strName; }
            set { strName = value; }
        }
    }
}

좋은 웹페이지 즐겨찾기