从本章节开始,所有的知识点只记录核心重点, 不做基础介绍,看知识点性质,偏向于理论则重理论
1.表达式目录树Expression
//1. 先看看委托 拉姆达表达式其实是作为委托的一个参数,本质是一个方法(匿名方法)
Func<int, int, int> func = (m, n) =>
{
int i = 0;
return m * n + 2;
};
//2.再看表达式目录树, 其数据结构--就像对一个计算做了一个精确的描述,展开之后发现,分为左边,右边,每个元素都可以把值都获取出来,二叉树
Expression<Func<int, int, int>> exp = (m, n) => m * n + 2;
//3.表达式目录树可以通过compile 转换成一个委托
var erpPlu= exp.Compile();
//4 注意:表达式目录树只能有一行;
Expression<Func<int, int, int>> exp1 = (m, n) =>
{
return m * n + 2;
};
//5. Invoke(执行) 表达式目录树:语法树,或者说是一种数据结构
int iResult1 = func.Invoke(12, 23);
int iResult2 = exp.Compile().Invoke(12, 23);
2.动态拼装表达式目录树
//1. 目标是拼装如下表达式,首先反编译后看看大概代码,然后从右边往后拆分替换,
Expression<Func<People, bool>> lambda = x => x.Id.ToString().Equals("5");
ParameterExpression parameterExpression = Expression.Parameter(typeof(People), "x");
FieldInfo idfield = typeof(People).GetField("Id");
var idExp = Expression.Field(parameterExpression, idfield);
//int i = 0;
//i.ToString();
MethodInfo toString = typeof(int).GetMethod("ToString", new Type[0]);
//Expression.Call()调用方法
var toStringExp = Expression.Call(idExp, toString, Array.Empty<Expression>());
var Equals = typeof(string).GetMethod("Equals", new Type[] { typeof(string) });
Expression expressionConstant5 = Expression.Constant("5", typeof(string));
var equalsExp = Expression.Call(toStringExp, Equals, new Expression[]
{
expressionConstant5
});
Expression<Func<People, bool>> expression = Expression.Lambda<Func<People, bool>>(equalsExp, new ParameterExpression[]
{
parameterExpression
});
//转成委托
Func<People, bool> func = expression.Compile();
//调用
var bResult = func.Invoke(new People()
{
Id = 5,
Name = "海贝"
});
//测试
new List<People>().AsQueryable().Where(expression);
3.基于Expression的扩展应用
现有一需求 把 people 转化成 peoplecopy
{
People people = new People()
{
Id = 11,
Name = "张三",
Age = 31
};
//不能转换,因为没有父子级关系;
PeopleCopy copy = (PeopleCopy)people;
//方式1 :硬编码性能最高,扩展性不好,
PeopleCopy peopleCopy = new PeopleCopy()
{
Id = people.Id,
Name = people.Name,
Age = people.Age
};
//方式2:反射,性能问题
PeopleCopy peopleCopy1 = ReflectionMapper.Trans<People, PeopleCopy>(people);
public class ReflectionMapper
{
/// <summary>
/// 反射
/// </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 itemOut in tOut.GetType().GetFields())
{
var fieldIn = tIn.GetType().GetField(itemOut.Name);
itemOut.SetValue(tOut, fieldIn.GetValue(tIn));
}
return tOut;
}
}
//方式3:序列化反序列化,性能问题;
PeopleCopy peopleCopy2 = SerializeMapper.Trans<People, PeopleCopy>(people);
/// <summary>
/// 使用第三方序列化反序列化工具
/// 还有automapper
/// </summary>
public class SerializeMapper
{
/// <summary>
/// 序列化反序列化方式
/// </summary>
/// <typeparam name="TIn"></typeparam>
/// <typeparam name="TOut"></typeparam>
public static TOut Trans<TIn, TOut>(TIn tIn)
{
return JsonConvert.DeserializeObject<TOut>(JsonConvert.SerializeObject(tIn));
}
}
}
//方式4:生成表达式目录树 字典缓存
PeopleCopy peopleCopy4 = ExpressionMapper.Trans<People, PeopleCopy>(people);
/// <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");
List<MemberBinding> memberBindingList = new List<MemberBinding>();
foreach (var item in typeof(TOut).GetProperties())
{
MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name));
//Bind 赋值
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);
}
}
///方式5: 生成表达式目录树 泛型缓存 (推荐性能最高,仅次于硬编码)
PeopleCopy peopleCopy6 = 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);
}
}