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








原创粉丝点击