/// <summary> /// 適用於初始化新實體 /// </summary> static public T RotationMapping<T, S>(S s) { T target = Activator.CreateInstance<T>(); var originalObj = s.GetType(); var targetObj = typeof(T); foreach (PropertyInfo original in originalObj.GetProperties()) { foreach (PropertyInfo t in targetObj.GetProperties()) { if (t.Name == original.Name) { t.SetValue(target, original.GetValue(s, null), null); } } } return target; } /// <summary> /// 適用於沒有新建實體之間 /// </summary> static public T RotationMapping<T, S>(T t, S s) { var originalObj = s.GetType(); var targetObj = typeof(T); foreach (PropertyInfo sp in originalObj.GetProperties()) { foreach (PropertyInfo dp in targetObj.GetProperties()) { if (dp.Name == sp.Name) { dp.SetValue(t, sp.GetValue(s, null), null); } } } return t; }
