通过Nuget包管理器添加Newtonsoft.Json
构造一个Trans的静态方法:
public static TOut Trans(TIn tIn)
{
return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(tIn));
}
前端调用:
People people = new People
{
Id = 1,
Name = "ddd",
Class = 1
};
PeopleCopy peopleCopy = SerializeMapper.Trans(people);
构造一个Trans的静态方法:
public static TOut Trans(TIn tIn)
{
TOut tOut = Activator.CreateInstance();
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);
静态字典:
public static Dictionary<string, Object> _Dictionary = new Dictionary<string, object>();
构造一个Trans的静态方法:
public static TOut Trans(TIn tIn)
{
TOut tOut = Activator.CreateInstance();
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 memberBindingsList = new List();
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> lambda = Expression.Lambda>(memberInitExpression, new ParameterExpression[]
{
parameterExpression
});
Func func = lambda.Compile();
_Dictionary[key] = func;
}
return ((Func)_Dictionary[key]).Invoke(tIn);
}
前端调用:
People people = new People
{
Id = 1,
Name = "ddd",
Class = 1
};
PeopleCopy peopleCopy = ExpressionMapper.Trans(people);
构造一个泛型缓存类:
public class ExpressionGenericMapper
{
private static Func _FUNC = null;
static ExpressionGenericMapper()
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn),"p");
List memberBindingsList = new List();
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> lambda = Expression.Lambda>(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);