C#反射使用方法過(guò)程及步驟
C#反射使用方法過(guò)程及步驟,供大家參考,具體內(nèi)容如下
1. 定義要訪問(wèn)類(lèi)的全名
2. 獲取該類(lèi)的類(lèi)型
3. 實(shí)例化該類(lèi)
4. 獲取該類(lèi)的字段、屬性,方法
5. 設(shè)置該字段或?qū)傩詢?nèi)容,或調(diào)用其方法
從而達(dá)到使用字符串訪問(wèn)相應(yīng)類(lèi)的目的。
示例:
1. 根據(jù)窗口類(lèi)的名稱,產(chǎn)生一個(gè)新的窗口,相當(dāng)于new 窗口類(lèi)
//1. 定義窗口類(lèi)名稱:(窗口類(lèi)的字符串名字,需要全路徑名,否則獲取不到TYPE)
string customClassName = @"IBAutoDeal.IBDealForms." + hasPanel.Text;
//2. 獲取字符串customClassName(某個(gè)窗口類(lèi)的字符串名字)指定類(lèi)的Type
Type customClassType = Type.GetType(customClassName);
//3. 生成指定Type的實(shí)例,相當(dāng)于new 類(lèi)
object customClassObj = Activator.CreateInstance(customClassType);
// 4. 對(duì)新生成的類(lèi)進(jìn)行操作, 本例把new 出的窗口賦值給一個(gè)窗口變量
panelForm = customClassObj as Form;
// 由此達(dá)到使用類(lèi)的字符串名生成一個(gè)該類(lèi)的實(shí)例供后續(xù)程序的使用
2. 根據(jù)類(lèi)的字符串名字,給該類(lèi)的字段或?qū)傩再x值
//反射功能使用及步驟:以下示例為給字符串類(lèi)名生成一個(gè)實(shí)例,并給該類(lèi)的屬性或字段賦值
// 1. 定義類(lèi)名稱:(類(lèi)的字符串名字,需要全路徑名,否則獲取不到TYPE)
string customVaribleName = @"IBAutoDeal.IBDealClass.CommVar";
// 2. 獲取字符串 customVaribleName指定類(lèi)的Type
Type customVaribleType = Type.GetType(customVaribleName);
// 3. 生成指定Type的實(shí)例,相當(dāng)于new 類(lèi)
object customAaribleObj = Activator.CreateInstance(customVaribleType);
// 4. 獲取該類(lèi)字段,本例是給一個(gè)公共的窗體變量賦值,而該變量在此是字段
// 如果是給該類(lèi)的屬性賦值,就需要使用 PropertyInfo pi = customVaribleType.GetField("v" + hasPanel.Text)
// 在該類(lèi)中,某個(gè)域是屬性還是字段呢?我個(gè)人的判斷是,若該域提供了GET,SET方法,就為屬性,否則為字段,也不知道正確否?
FieldInfo pi = customVaribleType.GetField("v" + hasPanel.Text);
// 5. 給該字段賦值,該字段是個(gè)窗口類(lèi)變量,customClassObj就是示例1中根據(jù)字符串產(chǎn)生的窗口類(lèi)
pi.SetValue(customAaribleObj, customClassObj);
// 通過(guò)以上5個(gè)步驟,完成了根據(jù)字符串的名字,給相應(yīng)類(lèi)的字段賦值
3. 根據(jù)類(lèi)的字符串名字,讀取字段值,并使用該字段值(示例中,該字段值是窗體,該示例是銷(xiāo)毀該窗體)
// 1. 定義類(lèi)名稱:(類(lèi)的字符串名字,需要全路徑名,否則獲取不到TYPE)
string customVaribleName = @"IBAutoDeal.IBDealClass.CommVar";
// 2. 獲取字符串 customVaribleName指定類(lèi)的Type
Type customVaribleType = Type.GetType(customVaribleName);
// 3. 生成指定Type的實(shí)例,相當(dāng)于new 類(lèi)
object customAaribleObj = Activator.CreateInstance(customVaribleType);
// 4. 獲取該類(lèi)字段,本例是給一個(gè)公共的窗體變量賦值,而該變量在此是字段
// 如果是給該類(lèi)的屬性賦值,就需要使用 PropertyInfo pi = customVaribleType.GetField("v" + hasPanel.Text)
// 在該類(lèi)中,某個(gè)域是屬性還是字段呢?我個(gè)人的判斷是,若該域提供了GET,SET方法,就為屬性,否則為字段,也不知道正確否?
FieldInfo pi = customVaribleType.GetField("v" + e.Panel.Name);
// 5. 讀取該字段的值(本示例該字段值為窗體,讀取窗體變量,把銷(xiāo)毀該窗體)
(pi.GetValue(customAaribleObj) as Form).Dispose();
// 6. 再為該字段賦空值
pi.SetValue(customAaribleObj, null);
4. 示例3原來(lái)是通過(guò)switch來(lái)判斷需要關(guān)閉那個(gè)窗口,這樣就會(huì)有很多的case語(yǔ)句,通過(guò)反射,就用示例3的6行代碼取代了,下面貼出原程序的代碼,目的是讓大家了解反射的作用:
// 被示例3取代的原代碼段(根據(jù)e.Panel.Name的值對(duì)相應(yīng)窗口關(guān)閉,并給公用變量賦值)
switch (e.Panel.Name)
{
case "FrmAccountStatistics":
CommVar.vFrmAccountStatistics.Dispose();
CommVar.vFrmAccountStatistics = null;
break;
case "frmPositionManager":
CommVar.vfrmPositionManager.Dispose();
CommVar.vfrmPositionManager = null;
break;
case "frmTrader":
CommVar.vfrmTrader.Dispose();
CommVar.vfrmTrader = null;
break;
case "frmLog":
CommVar.vfrmLog.Dispose();
CommVar.vfrmLog = null;
break;
case "frmDataTransPlant":
CommVar.vfrmDataTransPlate.Dispose();
CommVar.vfrmDataTransPlate = null;
break;
case "frmAccountTJ":
CommVar.vfrmAccountTJ.Dispose();
CommVar.vfrmAccountTJ = null;
break;
case "frmGridViewConfig":
CommVar.vfrmGridViewConfig.Dispose();
CommVar.vfrmGridViewConfig = null;
break;
case "frmTaticlistExcel":
CommVar.vfrmTaticlistExcel.Dispose();
CommVar.vfrmTaticlistExcel = null;
break;
case "frmQuoteLst":
CommVar.vfrmQuoteLst.Dispose();
CommVar.vfrmQuoteLst = null;
break;
case "frmProduct":
CommVar.vfrmProduct.Dispose();
CommVar.vfrmProduct = null;
break;
default:
break;
}
5. 針對(duì) T 的泛類(lèi)型定義的類(lèi),給屬性賦值示例代碼如下:
public static T ReadClass<T>(string pClassName) where T:class
{
//1. 定義類(lèi)名稱:(因?yàn)橛袀魅氲腡類(lèi),所以不需要類(lèi)的名稱了)
//2.獲取類(lèi)T的TYPE,注意,前面例子中是通過(guò)字符串取類(lèi)型,使用的是Type.GetType()
Type classType = typeof(T);
//3. 生成指定 T 的實(shí)例,前面例子中,我們使用的是Activator.CreateInstance(classType)
T ClassObj = Activator.CreateInstance<T>();
//4. 定義類(lèi)的屬性變量,因程序要多次讀取不同的屬性,在此定義一個(gè)局部變量
PropertyInfo classPropertyInfo = null;
string tableName = "TradeParameters";
//DataTable必須轉(zhuǎn)成DataView才能使用RowFilter
DataTable vDt = DataTableHelper.ReadTable(tableName);
DataView vDv = vDt.DefaultView;
vDv.RowFilter = $"ClassName = '{pClassName}'";
if (vDv.Count > 0)
{
string pName, pVaule;
foreach(DataRow dr in vDv)
{
pName = dr["AttributeName"].ToString();
pVaule = dr["AttributeValue"].ToString();
//5. 獲取指定名稱屬性
classPropertyInfo = classType.GetProperty(pName);
if (classPropertyInfo != null)
{
//6. 給指定屬性賦值
classPropertyInfo.SetValue(ClassObj, pVaule);
}
}
}
return ClassObj;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#棧變化規(guī)則圖解示例(棧的生長(zhǎng)與消亡)
多數(shù)情況下我們不需要關(guān)心棧的變化,下文會(huì)給出一個(gè)具體的示例。另外,理解棧的變化對(duì)于理解作用域也有一定的好處,因?yàn)镃#的局部變量作用域是基于棧的。2013-11-11
C# DataGridView中實(shí)現(xiàn)勾選存儲(chǔ)數(shù)據(jù)和右鍵刪除數(shù)據(jù)(示例代碼)
這篇文章主要介紹了C# DataGridView中實(shí)現(xiàn)勾選存儲(chǔ)數(shù)據(jù)和右鍵刪除數(shù)據(jù)的示例代碼,通過(guò)示例代碼給大家展示運(yùn)行效果圖,需要的朋友可以參考下2021-07-07
C#計(jì)算輸入漢字GBK編碼后十六進(jìn)制數(shù)輸出的方法
這篇文章主要介紹了C#計(jì)算輸入漢字GBK編碼后十六進(jìn)制數(shù)輸出的方法,涉及C#編碼操作相關(guān)技巧,需要的朋友可以參考下2015-04-04
C#獲取Excel文件所有文本數(shù)據(jù)內(nèi)容的示例代碼
獲取上傳的?EXCEL?文件的所有文本信息并存儲(chǔ)到數(shù)據(jù)庫(kù)里,可以進(jìn)一步實(shí)現(xiàn)對(duì)文件內(nèi)容資料關(guān)鍵字查詢的全文檢索,有助于我們定位相關(guān)文檔,本文詳細(xì)介紹了C#獲取Excel文件所有文本數(shù)據(jù)內(nèi)容實(shí)現(xiàn)步驟和代碼,需要的朋友可以參考下2024-07-07
C# WinForm中Panel實(shí)現(xiàn)用鼠標(biāo)操作滾動(dòng)條的實(shí)例方法
由于在WinForm中Panel不能直接響應(yīng)鼠標(biāo)的滾動(dòng)事件,只好采用捕獲窗體的滾動(dòng)事件。2013-03-03

