1         public static T2 CopyToModel<T1, T2>(T1 source)
 2         {
 3             T2 model = default(T2);
 4             PropertyInfo[] pi = typeof(T2).GetProperties();
 5             PropertyInfo[] pi1 = typeof(T1).GetProperties();
 6             model = Activator.CreateInstance<T2>();
 7             for (int i = 0; i < pi.Length; i++)
 8             {
 9                 pi[i].SetValue(model, pi1[i].GetValue(source, null), null);
10             }
11             return model;
12         }
13 
14         public static List<T2> CopyToList<T1, T2>(List<T1> source)
15         {
16             List<T2> t2List = new List<T2>();
17             T2 model = default(T2);
18             PropertyInfo[] pi = typeof(T2).GetProperties();
19             PropertyInfo[] pi1 = typeof(T1).GetProperties();
20             foreach (T1 t1Model in source)
21             {
22                 model = Activator.CreateInstance<T2>();
23                 for (int i = 0; i < pi.Length; i++)
24                 {
25                     pi[i].SetValue(model, pi1[i].GetValue(t1Model, null), null);
26                 }
27                 t2List.Add(model);
28             }
29             return t2List;
30         }

 

相关文章:

  • 2022-12-23
  • 2021-06-26
  • 2021-09-11
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
猜你喜欢
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案