生成表达式目录树 缓存
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ExpressionDemo.MappingExtend
{
/// <summary>
/// 生成表达式目录树 缓存
/// </summary>
public class ExpressionMapper
{
/// <summary>
/// 字典缓存--hash分布
/// </summary>
private static Dictionary<string, object> _Dic = new Dictionary<string, object>();
/// <summary>
/// 字典缓存表达式树
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
/// <param name="tIn"></param>
/// <returns></returns>
public static TOut Trans<TIn, TOut>(TIn tIn)
{
//string key = string.Format("funckey_{0}_{1}", typeof(TIn).FullName, typeof(TOut).FullName);
//if (!_Dic.ContainsKey(key))
//{
// ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");// p=>(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<TIn, TOut> func = lambda.Compile();//拼装是一次性的
// _Dic[key] = func;
//}
//return ((Func<TIn, TOut>)_Dic[key]).Invoke(tIn);
{
string key = string.Format("funckey_{0}_{1}", typeof(TIn).FullName, typeof(TOut).FullName);
if (!_Dic.ContainsKey(key))
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
// 遍历属性
foreach (var item in typeof(TOut).GetProperties())
{
MemberExpression propertyExpression = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
MemberBinding memberBinding = Expression.Bind(item, propertyExpression);
memberBindingList.Add(memberBinding);
}
// 遍历字段
foreach (var item in typeof(TOut).GetFields())
{
MemberExpression fieldExpression = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name));
MemberBinding fieldBinding = Expression.Bind(item, fieldExpression);
memberBindingList.Add(fieldBinding);
}
var body = Expression.MemberInit(Expression.New(typeof(PeopleCopy)), memberBindingList.ToArray());
Expression<Func<People, PeopleCopy>> expression = Expression.Lambda<Func<People, PeopleCopy>>(body, new ParameterExpression[]
{
parameterExpression
});
_Dic[key] = expression.Compile();
}
return ((Func<TIn, TOut>)_Dic[key]).Invoke(tIn);
}
}
}
}