【转】C#基础概念之“什么是反射?”
什么是反射?
答: 反射,Reflection,通過(guò)它我們可以在運(yùn)行時(shí)獲得各種信息,如程序集、模塊、類(lèi)型、字段、屬性、方法和事件.
通過(guò)對(duì)類(lèi)型動(dòng)態(tài)實(shí)例化后,還可以對(duì)其執(zhí)行操作。 簡(jiǎn)單來(lái)說(shuō)就是用string可以在runtime為所欲為的東西,實(shí)際上就是一個(gè).net framework內(nèi)建的萬(wàn)能工廠。
一般用于插件式框架程序和設(shè)計(jì)模式的實(shí)現(xiàn),當(dāng)然反射是一種手段可以充分發(fā)揮其能量來(lái)完成你想做的任何事情(前面好象見(jiàn)過(guò)一位高人用反射調(diào)用一個(gè)官方類(lèi)庫(kù)中未說(shuō)明的函數(shù)。。。)
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example25Lib
{
??? public class Class1
??? {
??????? private string name;
??????? private int age;
??????? //如果顯式的聲明了無(wú)參數(shù)構(gòu)造函數(shù),客戶(hù)端只需要用程序集的CreateInstance即可實(shí)例化該類(lèi)
??????? //在此特意不實(shí)現(xiàn),以便在客戶(hù)調(diào)用端體現(xiàn)構(gòu)造函數(shù)的反射實(shí)現(xiàn)
??????? //public Class1()
??????? //{
??????? //}
??????? public Class1(string Name, int Age)
??????? {
??????????? name = Name;
??????????? age = Age;
??????? }
??????? public void ChangeName(string NewName)
??????? {
??????????? name = NewName;
??????? }
??????? public void ChangeAge(int NewAge)
??????? {
??????????? age = NewAge;
??????? }
??????? public override string ToString()
??????? {
??????????? return string.Format("Name: {0}, Age: {1}", name, age);
??????? }
??? }
}
反射實(shí)例化對(duì)象并調(diào)用其方法,屬性和事件的反射調(diào)用略
?
using System;
using System.Collections.Generic;
using System.Text;
//注意添加該反射的命名空間
using System.Reflection;
namespace Example25
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? //加載程序集
??????????? Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll");
??????????? //遍歷程序集內(nèi)所有的類(lèi)型,并實(shí)例化
??????????? Type[] tmpTypes = tmpAss.GetTypes();
??????????? foreach (Type tmpType in tmpTypes)
??????????? {
??????????????? //獲取第一個(gè)類(lèi)型的構(gòu)造函數(shù)信息
??????????????? ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
??????????????? foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
??????????????? {
??????????????????? //為構(gòu)造函數(shù)生成調(diào)用的參數(shù)集合
??????????????????? ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();
??????????????????? object[] tmpParams = new object[tmpParamInfos.Length];
??????????????????? for (int i = 0; i < tmpParamInfos.Length; i++)
??????????????????? {
??????????????????????? tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
??????????????????????? if (tmpParamInfos[i].ParameterType.FullName == "System.String")
??????????????????????? {
??????????????????????????? tmpParams[i] = "Clark";
??????????????????????? }
??????????????????? }
??????????????????? //實(shí)例化對(duì)象
??????????????????? object tmpObj = tmpConsInfo.Invoke(tmpParams);
??????????????????? Console.WriteLine(tmpObj);
??????????????????? //獲取所有方法并執(zhí)行
??????????????????? foreach (MethodInfo tmpMethod in tmpType.GetMethods())
??????????????????? {
??????????????????????? //為方法的調(diào)用創(chuàng)建參數(shù)集合
??????????????????????? tmpParamInfos = tmpMethod.GetParameters();
??????????????????????? tmpParams = new object[tmpParamInfos.Length];
??????????????????????? for (int i = 0; i < tmpParamInfos.Length; i++)
??????????????????????? {
??????????????????????????? tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
??????????????????????????? if (tmpParamInfos[i].ParameterType.FullName == "System.String")
??????????????????????????? {
??????????????????????????????? tmpParams[i] = "Clark Zheng";
??????????????????????????? }
??????????????????????????? if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")
??????????????????????????? {
??????????????????????????????? tmpParams[i] = 27;
??????????????????????????? }
??????????????????????? }
??????????????????????? tmpMethod.Invoke(tmpObj, tmpParams);
??????????????????? }
?????????????????? //調(diào)用完方法后再次打印對(duì)象,比較結(jié)果
??????????????????? Console.WriteLine(tmpObj);
??????????????? }
??????????? }
??????????? Console.ReadLine();
??????? }
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/JaneTang/archive/2009/12/09/1620627.html
總結(jié)
以上是生活随笔為你收集整理的【转】C#基础概念之“什么是反射?”的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 思科系统公司以IP网络为中心的视频监视系
- 下一篇: silverlight旋转中心很好玩