考虑以下2类
public class UserType1
{
public DateTime Created { get; set; }
public string First { get; set; }
public Gender Genter { get; set; }
public int Id { get; set; }
public string Last { get; set; }
public DateTime Updated { get; set; }
public string DontMatchType { get; set; }
public string Unique1 { get; set; }
}
public class UserType2
{
public DateTime Created { get; set; }
public string First { get; set; }
public Gender Genter { get; set; }
public int Id { get; set; }
public string Last { get; set; }
public DateTime Updated { get; set; }
public int DontMatchType { get; set; }
public string Unique2 { get; set; }
}
和下面的代码
UserType1 user1 = new UserType1
{
Id = 1,
First = "John",
Last = "Doe",
Genter = Gender.Male,
Created = DateTime.Now.AddDays(-1),
Updated = DateTime.Now,
DontMatchType = "won't map",
Unique1 = "foobar"
};
UserType2 user2 = CopyTo<UserType2>(user1);
你可以看到这个映射功能将只地图匹配名称/类型
public static T CopyTo<T>(object source)
where T : new()
{
if (source == null) throw new ArgumentException("surce is null", "source");
T target = new T();
source.GetType()
.GetProperties()
.Join(target.GetType().GetProperties()
, s => s.Name
, t => t.Name
, (s, t) => new
{
source = s,
target = t
})
.AsParallel()
.Where(inCommon => inCommon.source.PropertyType == inCommon.target.PropertyType
&& inCommon.source.CanRead && inCommon.target.CanWrite)
.ForAll(inCommon => inCommon.target.SetValue(target, inCommon.source.GetValue(source, null), null));
return target;
}
,你可以使用
public static IEnumerable<T> CopyTo<T>(IEnumerable<object> source)
where T : new()
{
return source.AsParallel().Select(CopyTo<T>);
}
复制收集这样
UserType1[] users1 = new[]
{
new UserType1
{
...
}
};
UserType2[] users2 = CopyTo<UserType2>(users1).ToArray();
这样做,它不会无谓地循环对象B为对象的每个属性的所有属性,其使用连接到额外的好处找到共同的属性(按名称和类型)。
映射可能会非常棘手。你可以进入很多名称/类型和嵌套的情况。我建议Automapper作为jjchiw提到。
你可以使用AutoMapper http://automapper.org/ – jjchiw
你的对象序列化? – binard