Popular blog tags
1,相关的两个类如下:现在我们知道短信的值,如何给微信来赋值呢,需要用到我们的反射,当然字段一一赋值也可以,但是如果字段有100个,这个方法就不行啦 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /// <summary> /// 短信 /// </summary> public class DuanXin {     public string RECEIVE_Number { get; set; }     public string RECEIVE_LOGIN_NAME { get; set; }     public string RECEIVE_PERSON_NAME { get; set; } } /// <summary> /// 微信 /// </summary> public class WeChat {     public string RECEIVE_Number { get; set; }     public string RECEIVE_LOGIN_NAME { get; set; }     public string RECEIVE_PERSON_NAME { get; set; } } 下面是解决办法   1 //解决问题,两个类字段相同,但是类名不一样,如何进行赋值,通过反射进行逐一字段读取然后赋值 2 var DuanXinType = new DuanXin(); 3 DuanXinType.RECEIVE_LOGIN_NAME = "huchao"; 4 DuanXinType.RECEIVE_Number = "15897654321"; 5 var typeduanxin = typeof(DuanXin); 6 var PropertyInfoDuanxin = typeduanxin.GetProperties(); 7 WeChat pWeChat = new WeChat(); 8 var typeWeChat = typeof(WeChat); 9 var PropertyInfosWeChat = typeWeChat.GetProperties(); 10 foreach (var itemduanxin in PropertyInfoDuanxin) 11 { 12 var objDuanXinType = itemduanxin.GetValue(DuanXinType); 13 foreach (var itemWeChat in PropertyInfosWeChat) 14 { 15 if (itemduanxin.Name == itemWeChat.Name) 16 { 17 itemWeChat.SetValue(pWeChat, objDuanXinType, null); 18 } 19 } 20 } .