通过Nuget包管理器添加Newtonsoft.Json
构造一个Trans的静态方法:
public static TOut Trans<TIn,TOut>(TIn tIn)
{
return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));
}
前端调用:
People people = new People
{
Id = 1,
Name = "ddd",
Class = 1
};
PeopleCopy peopleCopy = SerializeMapper.Trans<People, PeopleCopy>(people);
构造一个Trans的静态方法:
public static TOut Trans<TIn,TOut>(TIn tIn)
{
TOut tOut = Activator.CreateInstance<TOut>();
foreach (var outfield in tOut.GetType().GetFields())
{
foreach (var infield in tIn.GetType().GetFields())
{
if (outfield.Name.Equals(infield.Name))
{
outfield.SetValue(tOut, infield.GetValue(tIn));
break;
}
}
}
foreach (var outProperty in tOut.GetType().GetProperties())
{
foreach (var inProperty in tIn.GetType().GetProperties())
{
if (outProperty.Name.Equals(inProperty.Name))
{
outProperty.SetValue(tOut,inProperty.GetValue(tIn));
break;
}
}
}
return tOut;
}
前端调用:
People people = new People
{
Id = 1,
Name = "ddd",
Class = 1
};
PeopleCopy peopleCopy = ReflectionMapper.Trans<People, PeopleCopy>(people);
静态字典:
public static Dictionary<string, Object> _Dictionary = new Dictionary<string, object>();
构造一个Trans的静态方法:
public static TOut Trans<TIn, TOut>(TIn tIn)
{
TOut tOut = Activator.CreateInstance<TOut>();
string key = string.Format("funckey_{0}_{1}", tIn.GetType().Name, tOut.GetType().Name);
if (!_Dictionary.ContainsKey(key))
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
List<MemberBinding> memberBindingsList = 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);
memberBindingsList.Add(memberBinding);
}
foreach (var item in typeof(TOut).GetFields())
{
MemberExpression filed = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
MemberBinding memberBinding = Expression.Bind(item, filed);
memberBindingsList.Add(memberBinding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingsList.ToArray());
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
Func<TIn, TOut> func = lambda.Compile();
_Dictionary[key] = func;
}
return ((Func<TIn, TOut>)_Dictionary[key]).Invoke(tIn);
}
前端调用:
People people = new People
{
Id = 1,
Name = "ddd",
Class = 1
};
PeopleCopy peopleCopy = ExpressionMapper.Trans<People, PeopleCopy>(people);
构造一个泛型缓存类:
public class ExpressionGenericMapper<TIn,TOut>
{
private static Func<TIn, TOut> _FUNC = null;
static ExpressionGenericMapper()
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn),"p");
List<MemberBinding> memberBindingsList = 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);
memberBindingsList.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);
memberBindingsList.Add(memberBinding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingsList.ToArray());
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
_FUNC = lambda.Compile();//拼装是一次性的
}
public static TOut Trans(TIn tIn)
{
return _FUNC(tIn);
}
}
前端调用:
People people = new People
{
Id = 1,
Name = "ddd",
Class = 1
};
PeopleCopy peopleCopy = ExpressionGenericMapper.Trans<People, PeopleCopy>(people);
原文地址:https://blog.csdn.net/m0_37591671/article/details/80386122
原文:https://www.cnblogs.com/jpfss/p/10898536.html