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; }
}
}

简化版

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);
}
}
}

实质语句只有一句:

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

C#通过属性名称获取(读取)属性值的方法 z的更多相关文章

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

    之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复 ...

  2. python 分享一个通过 (key1.key2.key3) 形式获取嵌套字典值的方法

    最近在做接口自动化测试,响应的内容大多数是多层嵌套的json数据,如果一层层的去剥,效率不高,脚本繁重,所以写了一个可以通过(key1.key2.key3)形式获取嵌套字典值的方法,如有不对或者需要优 ...

  3. 【ABAP系列】SAP ABAP获取域(domain)值的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP获取域(doma ...

  4. jquery通过name,id名称获取当前value值

    name是input标签的属性值,jQuery提供了attr() 方法用于设置/改变属性值 $("input:text").attr("name");$(&qu ...

  5. jQuery获取多种input值的方法

    1 if($("input[name=item][value='val']").attr('checked')==true) //判断是否已经打勾 name即控件name属性,va ...

  6. Query获取多种input值的方法

    1 if($("input[name=item][value='val']").attr('checked')==true) //判断是否已经打勾 name即控件name属性,va ...

  7. [转载]jquery获取元素索引值index()方法:

    jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的 ...

  8. jquery获取元素索引值index()方法

    jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的 ...

  9. Ionic3在ts中获取html中值的方法

    我觉得有两种方法,都是Angular中的语法,一种是把值当做参数传递,另一种是使用ngModel实现双向绑定 还有一种很少用到的,Js的原生方法:document.getElementById('ch ...

随机推荐

  1. HTML5新增标签及具体用法

    html5自从推广普及以来,迅速被各大浏览器支持.采用html5设计的网页逐渐成为网页设计的时尚.下面就温习下html5的新增标签. HTML 5 中的新特性包括了嵌入音频.视频和图形的功能,客户端数 ...

  2. Tomcat启动超过45S

    Timeout waiting for Tomcat v5.5&nbspServer @localhost  to start. Server did not start after 45s ...

  3. java 读写properties

    网速不好:先贴上资料: Java配置文件Properties的读取.写入与更新操作 [Spring] - Property注入 http://www.360doc.com/content/14/073 ...

  4. jquery 无缝滚动 jquery.kxbdmarquee

    DEMO http://code.ciaoca.com/jquery/kxbdmarquee/demo/ 官网 http://code.ciaoca.com/jquery/kxbdmarquee/ D ...

  5. 手把手原生js简单轮播图

    在团队带人,突然被人问到轮播图如何实现,进入前端领域有一年多了,但很久没自己写过,一直是用大牛写的插件,今天就写个简单的适合入门者学习的小教程.当然,轮播图的实现原理与设计模式有很多种,我这里讲的是用 ...

  6. 通过BroadCast与service时时监听网络变化

    首先需要一个service: 这里我定义了一个NetworkStateService,在这个service中我写了一个BroadcastReceiver用于监听网络状态发生改变的情况并在这个servi ...

  7. ubuntu安装opencv

    ubuntu版本是12.04 ,opencv的版本是2.4.9 其实官网有教程的,http://docs.opencv.org/doc/tutorials/introduction/linux_ins ...

  8. eclipse修改web项目部署路径

    Eclipse中用Tomcat发布的Web项目,更改其部署路径 我的Eclipse的工作目录是D:/workspace 先配置Tomcat 选择你的tomcat版本 点击next 这里先不要把项目添加 ...

  9. java的三大框架(一)

    现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2,Spring,Hibernate.似乎这些框架成为了一个人是否精通Java,是否会写J2EE程序的唯一事实标准和 ...

  10. MIPS指令集相关

    寄存器: 寄存器号            符号名            用途 0                 始终为0     看起来象浪费,其实很有用 1                 at  ...