将一个对象相同的属性(不区分大小写)赋值给一个新对象
程序员文章站
2022-06-22 18:49:56
1 public static T Mapper(S source) 2 { 3 T t = Activator.CreateInstance(); 4 try 5 { 6 var s_type = source.GetType(); 7 ... ......
1 public static T Mapper<S, T>(S source) 2 { 3 T t = Activator.CreateInstance<T>(); 4 try 5 { 6 var s_type = source.GetType(); 7 var t_type = typeof(T); 8 foreach (PropertyInfo sp in s_type.GetProperties()) 9 { 10 foreach (PropertyInfo dp in t_type.GetProperties()) 11 { 12 if (dp.Name.ToUpper() == sp.Name.ToUpper()) 13 { 14 dp.SetValue(t, sp.GetValue(source, null), null); 15 } 16 } 17 } 18 } 19 catch (Exception ex) 20 { 21 throw ex; 22 } 23 return t; 24 }
上一篇: Android 解压方法详情
下一篇: 5G学习笔记之F1AP