C# 通过属性名称获取(读取)Json对象属性值的方法
2017-01-06 16:16
721 查看
之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊!
然后今天我再上网找了找,被我找到了,跟大家分享一下。
其实原理并不复杂,就是通过反射利用属性名称去获取属性值,以前对反射不熟悉,所以没想到啊~
不得不说反射是一种很强大的技术。。
下面给代码,希望能帮到有需要的人。
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);
}
}
}
实质语句只有一句:
其他可以忽略。。
Object.GetType().GetProperty().GetValue()
不能直接读取Json转化对象 JsonConvert.DeserializeObject(JsonStr),必须有对应类型[b]JsonConvert.DeserializeObject<>(JsonStr),所以无意义[/b]
然后今天我再上网找了找,被我找到了,跟大家分享一下。
其实原理并不复杂,就是通过反射利用属性名称去获取属性值,以前对反射不熟悉,所以没想到啊~
不得不说反射是一种很强大的技术。。
下面给代码,希望能帮到有需要的人。
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);
其他可以忽略。。
Object.GetType().GetProperty().GetValue()
不能直接读取Json转化对象 JsonConvert.DeserializeObject(JsonStr),必须有对应类型[b]JsonConvert.DeserializeObject<>(JsonStr),所以无意义[/b]
相关文章推荐
- javaScript常用算法
- 小笔记3
- Extjs 前台获取后台传过来的Json字符串
- Extjs隐藏表单字段
- JavaScript笔记1
- jsp页面EL表达式不被解析的解决方法
- js屏蔽/过滤 特殊字符,输入就删除掉,实时删除,
- JSP中的FORM表单中只有一个input文本的时候,按回车键将会自动将表单提交
- 用js将long型数据转换成date型或datetime型
- 记录--JS静态分页使用情况
- JSF框架下使用searchableSelect实现搜索及级联功能
- 基于jsp的海鱼超市管理系统
- 【JS】表达式&语句
- Google Gson:POJO 和 JSON 的字段映射规则
- Extjs怎么实现页面值的传递和接受
- Javascript设计模式
- javascript之应用技巧
- JSP、Servlet实战:团队开发新闻系统(四)页面布局
- 让web API始终返回json格式的数据
- 用jstl输出带尖括号的内容时如何避免被html误解析