static class ExpressionGeneric<TIn,TOut>
{
private static Func<TIn,TOut> _Func;
static ExpressionGeneric()
{
ParameterExpression parameter = Expression.Parameter(typeof(TIn), "param");
List<MemberBinding> list = new List<MemberBinding>();
foreach(var item in typeof(TOut).GetProperties())
{
MemberExpression member = Expression.Property(parameter,typeof(TIn).GetProperty(item.Name));
MemberBinding binding = Expression.Bind(item, member);
list.Add(binding);
}
foreach(var item in typeof(TOut).GetFields())
{
MemberExpression member = Expression.Field(parameter,typeof(TIn).GetField(item.Name));
MemberBinding binding = Expression.Bind(item, member);
list.Add(binding);
}
MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), list);
Expression<Func<TIn, TOut>> Lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, parameter);
_Func = Lambda.Compile();
}
public static TOut Ttrans(TIn paramIn)
{
return _Func.Invoke(paramIn);
}
}