關(guān)于C#基礎(chǔ)知識回顧--反射(三)
但是,如果對象是在運行時動態(tài)創(chuàng)建的,反射的功能就顯示出來了。在這種情況下,需要首先獲取一個構(gòu)造函數(shù)列表,然后再調(diào)用列表中的某個構(gòu)造函數(shù),創(chuàng)建一個該類型的實例。通過這種機制,可以在運行時實例化任意類型的對象而不必在聲明中指定。
為了獲得某個類型的構(gòu)造函數(shù),需要調(diào)用Type對象上的GetConstructors()。常用形式為:
ConstructorInfo[] GetConstructors()
該方法返回一個描述構(gòu)造函數(shù)的ConstructorInfo對象數(shù)組。ConstructorInfo中常用的
是GetParamters()方法,該方法返回給定構(gòu)造函數(shù)的參數(shù)列表。
一旦找到了合適的構(gòu)造函數(shù),就調(diào)用ConstructorInfo定義的Invoke()方法來創(chuàng)建對象:
object Invoke(object[] args)
需要傳遞給此方法的所有參數(shù)都在args中指定。如果不需要參數(shù),args必須為null。另外,
args必須包含與參數(shù)個數(shù)相同的元素,并且實參的類型必須與形參的類型兼容。Invoke()方法返回
的是指向新構(gòu)造對象的引用。
例子:
測試對象類
class MyClass
{
int x;
int y;
public MyClass(int i)
{
Console.WriteLine("一個參數(shù)的構(gòu)造函數(shù):");
x = y = i;
}
public MyClass(int i, int j)
{
Console.WriteLine("兩個參數(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("類構(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("沒有找到兩個參數(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);
}
}
}
}
}
}
運行結(jié)果為:

本例中,找到了一個兩個參數(shù)的構(gòu)造函數(shù),那么使用下面的語句實例化了一個該類型的對象:
object reflectOb=ci[x].Invoke(consargs);
調(diào)用Invoke()方法后,reflectOb將引用一個MyClass類型的對象。此后,程序?qū)?zhí)行
reflectOb上的方法。
注意:本例為了簡單起見,假設(shè)了一個使用兩個參數(shù)的構(gòu)造函數(shù),并且兩個參數(shù)都為int類型。但在實際的應(yīng)用程序中,必須檢驗每一個參數(shù)的類型。
相關(guān)文章
C#實現(xiàn)winform自動關(guān)閉MessageBox對話框的方法
這篇文章主要介紹了C#實現(xiàn)winform自動關(guān)閉MessageBox對話框的方法,實例分析了C#中MessageBox對話框的相關(guān)操作技巧,需要的朋友可以參考下2015-04-04Js中的substring,substr與C#中的Substring比較
本篇文章主要是對Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C#中的自動類型轉(zhuǎn)換和強制類型轉(zhuǎn)換
這篇文章主要介紹了C#中的自動類型轉(zhuǎn)換和強制類型轉(zhuǎn)換,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-08-08使用策略模式實現(xiàn)報警服務(wù)示例詳解(短信報警)
服務(wù)的功能:這個服務(wù)就是能夠?qū)崿F(xiàn)多通路報警的服務(wù),比如郵件報警、客戶端報警、短信報警等,該服務(wù)靈活性還不錯,比較方便擴展2014-01-01