PropertyInfo的使用
来源:互联网 发布:地球为什么会自转 知乎 编辑:程序博客网 时间:2024/05/20 09:21
1.案例1,如果两个类中有大部分的字段相同,需要将其中一个类的字段赋值给另外一个类:
定义Person类:
public class Person { public Person(int id,string name,string address) { this.Id = id; this.Name = name; this.Address = address; } public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } }
定义User类
public class User { public int Id { get; set; } public string Name { get; set; } public string Group { get; set; } }转换方法:
public static User ConvertObject(User user,Person person) { PropertyInfo[] userPro = user.GetType().GetProperties(); PropertyInfo[] personPro = person.GetType().GetProperties(); if (userPro.Length>0&&personPro.Length>0) { for (int i = 0; i < userPro.Length; i++) { for (int j = 0; j < personPro.Length; j++) {<br> //判断User的属性是不是的Person中 if (userPro[i].Name == personPro[j].Name && userPro[i].PropertyType == personPro[j].PropertyType) { Object value=personPro[j].GetValue(person, null); //将Person中属性的值赋值给User<br> userPro[i].SetValue(user,value , null); } } } } return user; }方法的调用:
static void Main(string[] args) { Person person = new Person(1,"FlyElephant","北京"); User user = new User(); user.Id = 20; user = ConvertObject(user, person); Console.WriteLine("Id:" + user.Id + "Name:" + user.Name + "角色:" + user.Group); System.Console.Read(); }2.之前在做Winform的时候就经常回使用到SqlHelper,现在也有很多公司是这么使用的,当时很多东西感觉就是重复性的操作,一度以为编程只是复制粘贴,下面这段代码大家应该很常见:
List<Person> list = new List<Person>();SqlDataReader sdr = new SqlDataReader();while (sdr.Read()){ Person person = new Person(); person.Name = sdr.GetString(0); //....下面类似 list.Add(person);}换一种方式来实现上面的代码:
public static List<T> ConvertData<T>(SqlDataReader sdr) { List<T> list = new List<T>(); Type type = typeof(T); PropertyInfo[] properties = type.GetProperties(); while (sdr.Read()) { T model = Activator.CreateInstance<T>(); for (int i = 0; i < properties.Length; i++) { for (int j = 0; j < sdr.FieldCount; j++) { //判断属性的名称和字段的名称是否相同 if (properties[i].Name == sdr.GetName(j)) { Object value =sdr[j]; //将字段的值赋值给User中的属性 properties[i].SetValue(model, value, null); } } } list.Add(model); } return list; }调用:
List<User> list = new List<User>();SqlDataReader sdr = cmd.ExecuteReader();list = ConvertData<User>(sdr);
3.案例三,ajax页面传递值可以使用get方式,或者post方式的传递JSON格式数据转换
简单的转换一个get传递的字符串Name=xx&Age=xx,后台直接用一个字典去模拟了:
Dictionary<string, object> dic = new Dictionary<string, object>();dic.Add("Id",100);dic.Add("Name", "keso");dic.Add("Group", "程序员");转换字典方法:
public static T ConvertDic<T>(Dictionary<string, object> dic) { T model = Activator.CreateInstance<T>(); PropertyInfo[] modelPro = model.GetType().GetProperties(); if (modelPro.Length > 0 && dic.Count() > 0) { for (int i = 0; i < modelPro.Length; i++) { if (dic.ContainsKey(modelPro[i].Name)) { modelPro[i].SetValue(model, dic[modelPro[i].Name], null); } } } return model; }调用:
User user = ConvertDic<User>(dic);
阅读全文
0 0
- 属性PropertyInfo的使用
- PropertyInfo的使用
- C#中属性PropertyInfo的使用
- C#中属性PropertyInfo的使用
- C#中属性PropertyInfo的使用,Dictionary转为Model实例
- C# PropertyInfo的用法
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- C#遍历类的属性 PropertyInfo.Attributes
- 反射:PropertyInfo、FieldInfo和MemberInfo的区别
- [原创]让你省写大量重复代码的方法 使用PropertyInfo类 反射获取类 的类型
- [原创]让你省写大量重复代码的方法 使用PropertyInfo类 反射获取类 的类型
- 让你省写大量重复代码的方法 使用PropertyInfo类 反射获取类 的类型
- C#根据字符串得到静态类的属性(PropertyInfo)
- 对象PropertyInfo中 找到对象的指定特性
- PropertyInfo 理解
- 关于PropertyInfo setvalue()和PropertyInfo getvalue()
- Android加载本地网页
- java 模拟http请求(跨域解决方案)
- Spark---Reciever
- SDUT-3402
- linux鸟哥视频学习笔记04
- PropertyInfo的使用
- POJ-3107-Godfather
- HTTP 方法:GET 对比 POST
- Nuttx操作系统用户手册(阅读记录)(翻译)
- 高斯消元-解求精技术(Python)
- codeforces 894E
- POJ 1201 Intervals (差分约束)
- ctype.h
- IntelliJ IDEA 的 Content Root 概念