日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

反射学习笔记之动态创建对象和调用方法

發布時間:2025/7/14 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 反射学习笔记之动态创建对象和调用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態加載和靜態引用的程序集并不是同一個Assembly了。事實上,在.Net中,同一個應用程序域并不允許同時加載兩個相同的Assembly。即使加載了,也會認為是兩個不同的程序集。如果要同時加載兩個,則必須在不同的應用程序域中。可以通過AppDomain創建一個新的應用程序域,在其中動態加載;而原來的程序域則靜態添加引用。此時將會認為是同一個程序集。

猜測是如此。我需要測試。想到我最近作的Remoting。服務器端和客戶端正是兩個不同的應用程序域。于是我在服務器端通過Activator.CreateInstance()動態創建對象,返回object類型。
然后再客戶端靜態添加該對象的引用。(我在本地機上試驗,所以服務器端和客戶端加載的程序集完全一樣,包括路徑都相同)然后再客戶端通過Activator.GetObject()獲得服務器端動態創建的對象,再顯示進行強制轉換。果然,使正確的。

轉自:http://www.cnblogs.com/wayfarer/archive/2004/07/20/25968.html

?

?

使用無參數構造函數創建對象

System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();

Object obj = asm.CreateInstance("ReflectorDemo.Calculator", true);//true 說明是不是大小寫無關(Ignore Case)。

?System.Runtime.Remoting.ObjectHandle handler = Activator.CreateInstance(null, "ReflectorDemo.Calculator");//null為當前程序集

?Object obj = handler.Unwrap();

?

使用有參數構造函數創建對象

?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為當前程序集

??????????????????? //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調用方法

?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方法中,第一個參數說明了想要調用的方法名稱;第二個參數說明是調用方法(因為InvokeMember的功能非常強大,不光是可以調用方法,還可以獲取/設置 屬性、字段等。此枚舉的詳情可參看Part.2或者MSDN);第三個參數是Binder,null說明使用默認的Binder;第四個參數說明是在這個對象上(obj是Calculator類型的實例)進行調用;最后一個參數是數組類型,表示的是方法所接受的參數。

?

我們在看一下對于靜態方法應該如何調用:

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調用方法

//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調用靜態方法

?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); //也可以這樣

?

?

?

轉載于:https://www.cnblogs.com/johnwonder/archive/2010/02/24/1672961.html

總結

以上是生活随笔為你收集整理的反射学习笔记之动态创建对象和调用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。