反射学习笔记之动态创建对象和调用方法
動態(tài)加載和靜態(tài)引用的程序集并不是同一個Assembly了。事實上,在.Net中,同一個應(yīng)用程序域并不允許同時加載兩個相同的Assembly。即使加載了,也會認(rèn)為是兩個不同的程序集。如果要同時加載兩個,則必須在不同的應(yīng)用程序域中??梢酝ㄟ^AppDomain創(chuàng)建一個新的應(yīng)用程序域,在其中動態(tài)加載;而原來的程序域則靜態(tài)添加引用。此時將會認(rèn)為是同一個程序集。
猜測是如此。我需要測試。想到我最近作的Remoting。服務(wù)器端和客戶端正是兩個不同的應(yīng)用程序域。于是我在服務(wù)器端通過Activator.CreateInstance()動態(tài)創(chuàng)建對象,返回object類型。
然后再客戶端靜態(tài)添加該對象的引用。(我在本地機上試驗,所以服務(wù)器端和客戶端加載的程序集完全一樣,包括路徑都相同)然后再客戶端通過Activator.GetObject()獲得服務(wù)器端動態(tài)創(chuàng)建的對象,再顯示進行強制轉(zhuǎn)換。果然,使正確的。
轉(zhuǎn)自:http://www.cnblogs.com/wayfarer/archive/2004/07/20/25968.html
?
?
使用無參數(shù)構(gòu)造函數(shù)創(chuàng)建對象
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
Object obj = asm.CreateInstance("ReflectorDemo.Calculator", true);//true 說明是不是大小寫無關(guān)(Ignore Case)。
?System.Runtime.Remoting.ObjectHandle handler = Activator.CreateInstance(null, "ReflectorDemo.Calculator");//null為當(dāng)前程序集
?Object obj = handler.Unwrap();
?
使用有參數(shù)構(gòu)造函數(shù)創(chuàng)建對象
?System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
??????????? Object[] parameters = new Object[2];
??????????? parameters[0] = 3;
??????????? parameters[1] = 5;
??????????? Object obj = asm.CreateInstance("ReflectorDemo.Calculator", true, System.Reflection.BindingFlags.Default, null, parameters, null, null);
?
?// System.Runtime.Remoting.ObjectHandle oh = AppDomain.CurrentDomain.CreateInstance(System.Reflection.Assembly.GetExecutingAssembly().FullName, controlName);
??????????????????? //_currentControl = (Control)oh.Unwrap();
??????????????????? //System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
??????????????????? //Object obj = asm.CreateInstance("NavigateControl.NavigatePanel", true);
??????????????????? //System.Runtime.Remoting.ObjectHandle handler = Activator.CreateInstance(null, "NavigateControl.NavigatePanel");//null為當(dāng)前程序集
??????????????????? //Object obj = handler.Unwrap();
??????????????????? //System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
??????????????????? //Object obj = asm.CreateInstance("NavigateControl.NavigatePanel",true, System.Reflection.BindingFlags.Default, null,null, null, null);
??????????????????? Type t = typeof(NavigateControl.NavigatePanel);
??????????????????? Object obj = Activator.CreateInstance(t);
?
?
?System.Reflection.Assembly asm = System.Reflection.Assembly.Load("NavigateControl");
??????????????????? Object obj = asm.CreateInstance("NavigateControl.NavigatePanel");
?
?
?
1.使用InvokeMember調(diào)用方法
?Type t = typeof(ReflectorDemo.Calculator);//Calculator是類
??????????? int result = (int)t.InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, obj, null);
??????????? Console.WriteLine(String.Format("The result is {0}", result));
在InvokeMember方法中,第一個參數(shù)說明了想要調(diào)用的方法名稱;第二個參數(shù)說明是調(diào)用方法(因為InvokeMember的功能非常強大,不光是可以調(diào)用方法,還可以獲取/設(shè)置 屬性、字段等。此枚舉的詳情可參看Part.2或者MSDN);第三個參數(shù)是Binder,null說明使用默認(rèn)的Binder;第四個參數(shù)說明是在這個對象上(obj是Calculator類型的實例)進行調(diào)用;最后一個參數(shù)是數(shù)組類型,表示的是方法所接受的參數(shù)。
?
我們在看一下對于靜態(tài)方法應(yīng)該如何調(diào)用:
Object[] parameters2 = new Object[2];
parameters2[0] = 6;
parameters2[1] = 9;
t.InvokeMember("Add", BindingFlags.InvokeMethod, null, typeof(Calculator), parameters2);
輸出:
Invoke Static Method:
[Add]: 6 plus 9 equals to 15
?
?使用MethodInfo.Invoke調(diào)用方法
//Type t = typeof(ReflectorDemo.Calculator);
??????????? //System.Reflection.MethodInfo mi = t.GetMethod("Add", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
??????????? //int result = (int)mi.Invoke(obj, null);
?
使用MethodInfo調(diào)用靜態(tài)方法
?Type t = typeof(ReflectorDemo.Calculator);
??????????? Object[] parameters2 = new Object[2];
??????????? parameters2[0] = 6;
??????????? parameters2[1] = 9;
??????????? System.Reflection.MethodInfo mi = t.GetMethod("Add", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
?????????? //int result = (int)mi.Invoke(null, parameters2);
??????????? // int result? = (int)mi.Invoke(t, parameters2); //也可以這樣
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/johnwonder/archive/2010/02/24/1672961.html
總結(jié)
以上是生活随笔為你收集整理的反射学习笔记之动态创建对象和调用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript 学习笔记 之事件
- 下一篇: ListBox,CheckBoxList