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

服務(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产福利片在线 | 秋霞黄色片 | 国产一区国产二区国产三区 | 免费观看视频在线 | 亚洲日本aⅴ片在线观看香蕉 | 99久久精品国产片久人 | 厨房里摸着乳丰满在线观看 | 99在线观看视频免费精品9 | 1024毛片| 99视频全部看免费观 | 亚洲国产精品一区二区久久 | 美国大片成人性网 | 五月精品 | 亚洲女人国产香蕉久久精品 | 精品在线观看一区 | 亚洲一区二区三区深夜天堂 | x8x8在线永久免费观看 | 日本一区二区三区国产 | 久久成人免费大片 | 好奇害死猫在线观看 | 99久久99久久免费精品蜜桃 | 欧美一区二区三区大片 | 情趣内衣在线观看 | 1024国产精品视频观看 | 秋霞一级毛片 | 91精品啪在线观看国产91九色 | 娇妻与公陈峰姚瑶小说在线阅读 | 扒开女人屁股眼看个够 | 美女无遮挡 | 啪啪无尽3d动漫漫画免费网站 | 久久久无码精品亚洲A片软件 | 精品免费视在线视频观看 | 湖南美女被黑人4p到惨叫 | 出轨同学会免费观看 | 波多野结在线 | 国产精品毛片久久久久久久 | 久久久乱码精品亚洲日韩 | 丝瓜视频黄瓜视频 | 日韩在线观看网址 | 国产福利不卡视频在免费 | 日本偷拍xxxxxxww |