C#通過屬性名稱獲取(讀取)屬性值的方法
之前在開發(fā)一個(gè)程序,希望能夠通過屬性名稱讀取出屬性值,但是由于那時(shí)候不熟悉反射,所以并沒有找到合適的方法,做了不少的重復(fù)性工作??!
然后今天我再上網(wǎng)找了找,被我找到了,跟大家分享一下。
其實(shí)原理并不復(fù)雜,就是通過反射利用屬性名稱去獲取屬性值,以前對(duì)反射不熟悉,所以沒想到啊~
不得不說(shuō)反射是一種很強(qiáng)大的技術(shù)。。
下面給代碼,希望能幫到有需要的人。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PropertyNameGetPropertyValueDemo { class Program { static void Main(string[] args) { Person ps = new Person(); ps.Name = "CTZ"; ps.Age = 21; Demo dm = new Demo(); dm.Str = "String"; dm.I = 1; Console.WriteLine(ps.GetValue("Name")); Console.WriteLine(ps.GetValue("Age")); Console.WriteLine(dm.GetValue("Str")); Console.WriteLine(dm.GetValue("I")); } } abstract class AbstractGetValue { public object GetValue(string propertyName) { return this.GetType().GetProperty(propertyName).GetValue(this, null); } } class Person : AbstractGetValue { public string Name { get; set; } public int Age { get; set; } } class Demo : AbstractGetValue { public string Str { get; set; } public int I { get; set; } } }
如果覺得上面比較復(fù)雜了,可以看下面的簡(jiǎn)化版。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GetValue { class Program { static void Main(string[] args) { Person ps = new Person(); ps.Name = "CTZ"; ps.Age = 21; Console.WriteLine(ps.GetValue("Name")); Console.WriteLine(ps.GetValue("Age")); } } class Person { public string Name { get; set; } public int Age { get; set; } public object GetValue(string propertyName) { return this.GetType().GetProperty(propertyName).GetValue(this, null); } } }
實(shí)質(zhì)語(yǔ)句只有一句:
this.GetType().GetProperty(propertyName).GetValue(this, null);
其他可以忽略。。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持我們!
上一篇:詳解二維碼生成工廠
欄 目:C#教程
下一篇:C# MVC 微信支付教程系列之公眾號(hào)支付代碼
本文標(biāo)題:C#通過屬性名稱獲取(讀取)屬性值的方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6082.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#通過Semaphore類控制線程隊(duì)列的方法
- 01-10C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
- 01-10C#實(shí)現(xiàn)獲取不同對(duì)象中名稱相同屬性的方法
- 01-10輕松學(xué)習(xí)C#的屬性
- 01-10C#通過正則表達(dá)式實(shí)現(xiàn)提取網(wǎng)頁(yè)中的圖片
- 01-10舉例講解C#中自動(dòng)實(shí)現(xiàn)的屬性
- 01-10詳解C#中的屬性和屬性的使用
- 01-10詳解C#中的接口屬性以及屬性訪問器的訪問限制


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)