3种方式将一个model 转换为另一个model

将一个model 转换为另一个model(字段一样)

PeopleCopy peopleCopy1 = ReflectionMapper.Trans<People, PeopleCopy>(people);//1 反射  性能2199
PeopleCopy peopleCopy2 = SerializeMapper.Trans<People, PeopleCopy>(people); //2 序列化反序列化 3599

 PeopleCopy peopleCopy = ExpressionGenericMapper<People, PeopleCopy>.Trans(people); //生成表达式目录树 泛型缓存-90

/// <summary>
///  Trans 反射
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
/// <param name="tIn"></param>
/// <returns></returns>
public static TOut Trans<TIn, TOut>(TIn tIn)
{
TOut tOut = Activator.CreateInstance<TOut>();
foreach (var itemOut in tOut.GetType().GetProperties())
{
var propIn = tIn.GetType().GetProperty(itemOut.Name);
itemOut.SetValue(tOut, propIn.GetValue(tIn));
//foreach (var itemIn in tIn.GetType().GetProperties())
//{
// if (itemOut.Name.Equals(itemIn.Name))
// {
// itemOut.SetValue(tOut, itemIn.GetValue(tIn));
// break;
// }
//}
}
foreach (var itemOut in tOut.GetType().GetFields())
{
var fieldIn = tIn.GetType().GetField(itemOut.Name);
itemOut.SetValue(tOut, fieldIn.GetValue(tIn));
//foreach (var itemIn in tIn.GetType().GetFields())
//{
// if (itemOut.Name.Equals(itemIn.Name))
// {
// itemOut.SetValue(tOut, itemIn.GetValue(tIn));
// break;
// }
//}
}
return tOut;
}

 

/// <summary>
/// Trans 序列化反序列化方式
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
public static TOut Trans<TIn, TOut>(TIn tIn)
{
return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));
}

 

-----------------------------------生成表达式目录树 泛型缓存-------------

调用: PeopleCopy peopleCopy = ExpressionGenericMapper<People, PeopleCopy>.Trans(people);

 

/// <summary>
/// 生成表达式目录树 泛型缓存
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
public class ExpressionGenericMapper<TIn, TOut>//Mapper`2
{
private static Func<TIn, TOut> _FUNC = null;
static ExpressionGenericMapper()
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
foreach (var item in typeof(TOut).GetFields())
{
MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
MemberBinding memberBinding = Expression.Bind(item, property);
memberBindingList.Add(memberBinding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray());
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
_FUNC = lambda.Compile();//拼装是一次性的
}
public static TOut Trans(TIn t)
{
return _FUNC(t);
}
}

posted @ 2021-01-24 21:40  大树2  阅读(666)  评论(0编辑  收藏  举报