一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#通過屬性名稱獲取(讀取)屬性值的方法

C#通過屬性名稱獲取(讀取)屬性值的方法

2021-12-14 13:59弎吩鍾熱℃ C#

本文主要介紹了C#通過屬性名稱獲取(讀取)屬性值的方法,并提供了簡化版代碼,具有很好的參考價(jià)值,需要的朋友可以看下

之前在開發(fā)一個(gè)程序,希望能夠通過屬性名稱讀取出屬性值,但是由于那時(shí)候不熟悉反射,所以并沒有找到合適的方法,做了不少的重復(fù)性工作啊!

然后今天我再上網(wǎng)找了找,被我找到了,跟大家分享一下。

其實(shí)原理并不復(fù)雜,就是通過反射利用屬性名稱去獲取屬性值,以前對反射不熟悉,所以沒想到啊~

不得不說反射是一種很強(qiáng)大的技術(shù)。。

下面給代碼,希望能幫到有需要的人。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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ù)雜了,可以看下面的簡化版。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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ì)語句只有一句:

this.GetType().GetProperty(propertyName).GetValue(this, null);

其他可以忽略。。

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持服務(wù)器之家!

原文鏈接:http://www.cftea.com/c/2012/10/5657.asp

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品网站在线观看 | 久久se视频精品视频在线 | 欧美一级欧美一级高清 | 天天欲色成人综合网站 | 无人在线观看免费高清视频播放 | 亚洲女bbwxxxx另类 | 日韩成人在线影院 | 日本亚洲欧洲高清有码在线播放 | 暖暖 免费 高清 日本 在线 | 成年女人免费 | 国产精品视频免费视频 | 色中文 | 国产91第一页 | 男人插女人软件 | 九九精品久久 | 精品国产自在现线拍400部 | 扒开双腿羞辱调教play视频 | 瘦老汉gay | 日本aa大片在线播放免费看 | 男人肌肌捅女人 | 国产自精品 | 2020年国产精品午夜福利在线观看 | 暴露狂婷婷医院暴露tx | 国产无套在线播放 | 男人懂得网站 | 欧美一级片免费在线观看 | 成人影音先锋 | 国产在线乱子伦一区二区 | 亚洲欧美韩国日产综合在线 | 男人的天堂视频 | 青青草原影院 | 精品一久久香蕉国产线看观 | video one 中国按摩 | 日韩色在线观看 | 出轨同学会免费观看 | a网在线| 欧美国产日本高清不卡 | 国产成人影院一区二区 | 精品国语对白精品自拍视 | brazzersxxx欧美| 免费观看在线 |