C#通过属性名称获取(读取)属性值的方法

本文介绍了一种使用C#反射技术根据属性名称获取属性值的方法。通过一个简单的示例展示了如何实现这一功能,这对于需要动态操作对象属性的场景非常有用。

之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊!

然后今天我再上网找了找,被我找到了,跟大家分享一下。

其实原理并不复杂,就是通过反射利用属性名称去获取属性值,以前对反射不熟悉,所以没想到啊~

不得不说反射是一种很强大的技术。。

下面给代码,希望能帮到有需要的人。

 
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. namespace PropertyNameGetPropertyValueDemo   
  7. {   
  8.     class Program   
  9.     {   
  10.         static void Main(string[] args)   
  11.         {   
  12.             Person ps = new Person();   
  13.             ps.Name = "CTZ";   
  14.             ps.Age = 21;   
  15.   
  16.             Demo dm = new Demo();   
  17.             dm.Str = "String";   
  18.             dm.I = 1;   
  19.   
  20.             Console.WriteLine(ps.GetValue("Name"));   
  21.             Console.WriteLine(ps.GetValue("Age"));   
  22.             Console.WriteLine(dm.GetValue("Str"));   
  23.             Console.WriteLine(dm.GetValue("I"));   
  24.         }   
  25.     }   
  26.   
  27.     abstract class AbstractGetValue   
  28.     {   
  29.         public object GetValue(string propertyName)   
  30.         {   
  31.             return this.GetType().GetProperty(propertyName).GetValue(thisnull);   
  32.         }   
  33.     }   
  34.   
  35.     class Person : AbstractGetValue     
  36.     {   
  37.         public string Name   
  38.         { getset; }   
  39.   
  40.         public int Age   
  41.         { getset; }   
  42.     }   
  43.   
  44.     class Demo : AbstractGetValue   
  45.     {   
  46.         public string Str   
  47.         { getset; }   
  48.   
  49.         public int I   
  50.         { getset; }   
  51.     }   
  52. }  

如果觉得上面比较复杂了,可以看下面的简化版。

 
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Text;   
  5.   
  6. namespace GetValue   
  7. {   
  8.     class Program   
  9.     {   
  10.         static void Main(string[] args)   
  11.         {   
  12.             Person ps = new Person();   
  13.             ps.Name = "CTZ";   
  14.             ps.Age = 21;   
  15.   
  16.             Console.WriteLine(ps.GetValue("Name"));   
  17.             Console.WriteLine(ps.GetValue("Age"));   
  18.         }   
  19.     }   
  20.   
  21.     class Person   
  22.     {   
  23.         public string Name   
  24.         { getset; }   
  25.   
  26.         public int Age   
  27.         { getset; }   
  28.   
  29.         public object GetValue(string propertyName)   
  30.         {   
  31.             return this.GetType().GetProperty(propertyName).GetValue(thisnull);   
  32.         }   
  33.     }   
  34. }  

实质语句只有一句:

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

其他可以忽略。。

Views: 255   Posted at: 2013-09-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值