반사 dll 획득 클래스 속성 방법

1614 단어 밑바닥
Assembly asb = Assembly.LoadFrom(Directory.GetCurrentDirectory() + "/Document.dll");
            //        Document 
            Type document = asb.GetType("Document.Document");
            //   Document   (         object  )
            object docObj = Activator.CreateInstance(document);
            //  Document     
            MethodInfo mi = document.GetMethod("GetName");
            //  
            object[] parameter = new object[] { "  " };
            Console.WriteLine(mi.Invoke(docObj, parameter).ToString());//        :  
            //  Document     
            FieldInfo pi = document.GetField("Name");
            Console.WriteLine(pi.GetValue(docObj));//       :  
            Console.ReadKey();


//          

            type.InvokeMember("Display",BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance , null , obj , new object[]{"aldfjdlf"});

 //         

            int i = (int)type.InvokeMember("GetInt",BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,null,obj,new object[]{1});

/     

            type.InvokeMember("Name",BindingFlags.SetProperty,null,obj,new string[]{"abc"});

            //     

            string str=(string)type.InvokeMember("Name",BindingFlags.GetProperty,null,obj,null);

//     

            type.InvokeMember("field1",BindingFlags.SetField,null,obj,new object[]{444});

 

            //     

            int f=(int)type.InvokeMember("field1",BindingFlags.GetField,null,obj,null);

좋은 웹페이지 즐겨찾기