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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

【转】C#基础概念之“什么是反射?”

發布時間:2025/3/21 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】C#基础概念之“什么是反射?” 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

什么是反射?

答: 反射,Reflection,通過它我們可以在運行時獲得各種信息,如程序集、模塊、類型、字段、屬性、方法和事件.

通過對類型動態實例化后,還可以對其執行操作。 簡單來說就是用string可以在runtime為所欲為的東西,實際上就是一個.net framework內建的萬能工廠。

一般用于插件式框架程序和設計模式的實現,當然反射是一種手段可以充分發揮其能量來完成你想做的任何事情(前面好象見過一位高人用反射調用一個官方類庫中未說明的函數。。。)

示例:

using System;
using System.Collections.Generic;
using System.Text;

namespace Example25Lib
{
??? public class Class1
??? {
??????? private string name;
??????? private int age;

??????? //如果顯式的聲明了無參數構造函數,客戶端只需要用程序集的CreateInstance即可實例化該類
??????? //在此特意不實現,以便在客戶調用端體現構造函數的反射實現
??????? //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);
??????? }
??? }
}

反射實例化對象并調用其方法,屬性和事件的反射調用略

?

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");

??????????? //遍歷程序集內所有的類型,并實例化
??????????? Type[] tmpTypes = tmpAss.GetTypes();
??????????? foreach (Type tmpType in tmpTypes)
??????????? {
??????????????? //獲取第一個類型的構造函數信息
??????????????? ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
??????????????? foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
??????????????? {
??????????????????? //為構造函數生成調用的參數集合
??????????????????? 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";
??????????????????????? }
??????????????????? }

??????????????????? //實例化對象
??????????????????? object tmpObj = tmpConsInfo.Invoke(tmpParams);
??????????????????? Console.WriteLine(tmpObj);

??????????????????? //獲取所有方法并執行
??????????????????? foreach (MethodInfo tmpMethod in tmpType.GetMethods())
??????????????????? {
??????????????????????? //為方法的調用創建參數集合
??????????????????????? 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);
??????????????????? }

?????????????????? //調用完方法后再次打印對象,比較結果
??????????????????? Console.WriteLine(tmpObj);
??????????????? }
??????????? }

??????????? Console.ReadLine();
??????? }
??? }
}

轉載于:https://www.cnblogs.com/JaneTang/archive/2009/12/09/1620627.html

總結

以上是生活随笔為你收集整理的【转】C#基础概念之“什么是反射?”的全部內容,希望文章能夠幫你解決所遇到的問題。

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