關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(三)
但是,如果對(duì)象是在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建的,反射的功能就顯示出來(lái)了。在這種情況下,需要首先獲取一個(gè)構(gòu)造函數(shù)列表,然后再調(diào)用列表中的某個(gè)構(gòu)造函數(shù),創(chuàng)建一個(gè)該類(lèi)型的實(shí)例。通過(guò)這種機(jī)制,可以在運(yùn)行時(shí)實(shí)例化任意類(lèi)型的對(duì)象而不必在聲明中指定。
為了獲得某個(gè)類(lèi)型的構(gòu)造函數(shù),需要調(diào)用Type對(duì)象上的GetConstructors()。常用形式為:
ConstructorInfo[] GetConstructors()
該方法返回一個(gè)描述構(gòu)造函數(shù)的ConstructorInfo對(duì)象數(shù)組。ConstructorInfo中常用的
是GetParamters()方法,該方法返回給定構(gòu)造函數(shù)的參數(shù)列表。
一旦找到了合適的構(gòu)造函數(shù),就調(diào)用ConstructorInfo定義的Invoke()方法來(lái)創(chuàng)建對(duì)象:
object Invoke(object[] args)
需要傳遞給此方法的所有參數(shù)都在args中指定。如果不需要參數(shù),args必須為null。另外,
args必須包含與參數(shù)個(gè)數(shù)相同的元素,并且實(shí)參的類(lèi)型必須與形參的類(lèi)型兼容。Invoke()方法返回
的是指向新構(gòu)造對(duì)象的引用。
例子:
測(cè)試對(duì)象類(lèi)
class MyClass
{
int x;
int y;
public MyClass(int i)
{
Console.WriteLine("一個(gè)參數(shù)的構(gòu)造函數(shù):");
x = y = i;
}
public MyClass(int i, int j)
{
Console.WriteLine("兩個(gè)參數(shù)構(gòu)造函數(shù):");
x = i;
y = j;
Show();
}
public int Sum()
{
return x + y;
}
public bool IsBetween(int i)
{
if (x < i && i < y)
return true;
else
return false;
}
public void Set(int a, int b)
{
Console.Write("函數(shù):Set(int a, int b)");
x = a;
y = b;
Show();
}
public void Set(double a, double b)
{
Console.Write("函數(shù):Set(double a, double b)");
x = (int)a;
y = (int)b;
Show();
}
public void Show()
{
Console.WriteLine("x:{0},y:{1}", x, y);
}
}
使用反射:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Reflection
{
class Program
{
static void Main(string[] args)
{
InvokeConsDemo();
Console.ReadKey();
}
static void InvokeConsDemo()
{
Type t = typeof(MyClass);
int val;
ConstructorInfo[] ci = t.GetConstructors();
Console.WriteLine("類(lèi)構(gòu)造函數(shù)如下:");
foreach (ConstructorInfo c in ci)
{
Console.Write("" + t.Name + "(");
ParameterInfo[] pi = c.GetParameters();
for (int i = 0; i < pi.Length; i++)
{
Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name);
if (i + 1 < pi.Length) Console.Write(", ");
}
Console.WriteLine(") ");
}
Console.WriteLine();
int x;
for (x = 0; x < ci.Length; x++)
{
ParameterInfo[] pi = ci[x].GetParameters();
if (pi.Length == 2) break;
}
if (x == ci.Length)
{
Console.WriteLine("沒(méi)有找到兩個(gè)參數(shù)的構(gòu)造函數(shù)"); return;
}
else
{
object[] consargs = new object[2];
consargs[0] = 10;
consargs[1] = 20;
object reflectOb = ci[x].Invoke(consargs);
Console.WriteLine("用reflectOb調(diào)用方法");
Console.WriteLine();
MethodInfo[] mi = t.GetMethods();
foreach (MethodInfo m in mi)
{
ParameterInfo[] pi = m.GetParameters();
if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(int))
{
object[] args = new object[2];
args[0] = 12;
args[1] = 7;
m.Invoke(reflectOb, args);
}
else if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(double))
{
object[] args = new object[2];
args[0] = 1.25;
args[1] = 7.5;
m.Invoke(reflectOb, args);
}
else if (m.Name.CompareTo("Sum") == 0)
{
val = (int)m.Invoke(reflectOb, null);
Console.WriteLine("Sum is {0}",val);
}
else if (m.Name.CompareTo("IsBetween") == 0)
{
object[] args = new object[1];
args[0] = 13;
if ((bool)m.Invoke(reflectOb, args))
{
Console.WriteLine("13 is between x and y");
}
}
else if (m.Name.CompareTo("Show") == 0)
{
m.Invoke(reflectOb, null);
}
}
}
}
}
}
運(yùn)行結(jié)果為:

本例中,找到了一個(gè)兩個(gè)參數(shù)的構(gòu)造函數(shù),那么使用下面的語(yǔ)句實(shí)例化了一個(gè)該類(lèi)型的對(duì)象:
object reflectOb=ci[x].Invoke(consargs);
調(diào)用Invoke()方法后,reflectOb將引用一個(gè)MyClass類(lèi)型的對(duì)象。此后,程序?qū)?zhí)行
reflectOb上的方法。
注意:本例為了簡(jiǎn)單起見(jiàn),假設(shè)了一個(gè)使用兩個(gè)參數(shù)的構(gòu)造函數(shù),并且兩個(gè)參數(shù)都為int類(lèi)型。但在實(shí)際的應(yīng)用程序中,必須檢驗(yàn)每一個(gè)參數(shù)的類(lèi)型。
- C#泛型和反射實(shí)例解析
- C#利用反射來(lái)判斷對(duì)象是否包含某個(gè)屬性的實(shí)現(xiàn)方法
- c#反射調(diào)用方法示例
- c#測(cè)試反射性能示例
- c#使用反射調(diào)用類(lèi)型成員示例
- c#反射機(jī)制學(xué)習(xí)和利用反射獲取類(lèi)型信息
- 關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(二)
- 關(guān)于C#基礎(chǔ)知識(shí)回顧--反射(一)
- C#反射在實(shí)際應(yīng)用中的實(shí)例代碼
- C#反射的一些應(yīng)用
- C#反射實(shí)例學(xué)習(xí)及注意內(nèi)容
- C#泛型實(shí)例詳解
- C#泛型編程介紹
- C#通過(guò)反射創(chuàng)建自定義泛型
相關(guān)文章
C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法
這篇文章主要介紹了C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法,實(shí)例分析了C#中MessageBox對(duì)話框的相關(guān)操作技巧,需要的朋友可以參考下2015-04-04Unity3D移動(dòng)端實(shí)現(xiàn)搖一搖功能
這篇文章主要為大家詳細(xì)介紹了基于Unity3D移動(dòng)端實(shí)現(xiàn)搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10Js中的substring,substr與C#中的Substring比較
本篇文章主要是對(duì)Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#使用Socket實(shí)現(xiàn)心跳的方法示例
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)心跳的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02C#中的自動(dòng)類(lèi)型轉(zhuǎn)換和強(qiáng)制類(lèi)型轉(zhuǎn)換
這篇文章主要介紹了C#中的自動(dòng)類(lèi)型轉(zhuǎn)換和強(qiáng)制類(lèi)型轉(zhuǎn)換,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-08-08使用策略模式實(shí)現(xiàn)報(bào)警服務(wù)示例詳解(短信報(bào)警)
服務(wù)的功能:這個(gè)服務(wù)就是能夠?qū)崿F(xiàn)多通路報(bào)警的服務(wù),比如郵件報(bào)警、客戶端報(bào)警、短信報(bào)警等,該服務(wù)靈活性還不錯(cuò),比較方便擴(kuò)展2014-01-01C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了C#強(qiáng)制轉(zhuǎn)換和嘗試轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09