反射エンティティクラスによって変換を達成するために、.NET二つの同一の構造

1  公共 静的 T2 CopyToModel <T1、T2> (T1源)
 2          {
 3              T2モデル= デフォルト(T2)。
4              PropertyInfo [] PI = typeof演算(T2).GetProperties();
5              PropertyInfo [] PI1 = typeof演算(T1).GetProperties();
6              モデル= Activator.CreateInstance <T2> ();
7              のためのint型 I = 0 ; iがpi.Lengthを<Iは++ 8              {
 9                  PI [I] .SetValue(モデル、PI1 [i]は以降.getvalue(ソース、NULL)、NULL );
10              }
 11              リターンモデル。
12          }
1つの         パブリック 静的リスト<T2> CopyToList <T1、T2>(一覧<T1> ソース)
 2          {
 3              一覧<T2> t2List = 新しいリスト<T2> ();
4              T2モデル= デフォルト(T2)。
5              PropertyInfo [] PI = typeof演算(T2).GetProperties();
6              PropertyInfo [] PI1 = typeof演算(T1).GetProperties();
7              のforeach(T1 t1Model におけるソース)
 8              {
 9                  モデル= Activator.CreateInstance <T2> ();
10                 以下のためにint型 i = 0 ; iはpi.Length <; iは++ 11                  {
 12                      PI [I] .SetValue(モデル、PI1、[I]以降.getvalue(t1Model ヌルNULL );
13                  }
 14                  t2List.Add(モデル)。
15              }
 16              リターンt2List。
17          }

 

おすすめ

転載: www.cnblogs.com/kliine/p/10978776.html