解析Expression
程序员文章站
2022-07-09 19:23:51
private static void GetExpression(Expression expression) { if (expression.NodeType != ExpressionType.Constant) { var _expression = expression as B ......
private static void getexpression<t2>(expression expression) { if (expression.nodetype != expressiontype.constant) { var _expression = expression as binaryexpression; if (_expression == null || expression.nodetype == expressiontype.lambda) { _expression = getbinaryexpression(expression); } if (anybinaryoperator(_expression.nodetype)) { var _leftexpression = _expression.left; getexpression<t2>(_leftexpression); var _rightexpression = _expression.right; getexpression<t2>(_rightexpression); } else { results.add(getexpressionresult<t2>(_expression)); } } }
/// <summary> /// escape to the lambda /// </summary> private static binaryexpression getbinaryexpression(expression expression) { lambdaexpression lambda = expression as lambdaexpression; var lambdaexpression = lambda.body; return lambdaexpression as binaryexpression; }
/// <summary> /// get result /// </summary> private static expressionresult getexpressionresult<t2>(binaryexpression expression) { var left_name = getleftexpression<t2>(expression); var right_name = getrightexpression(expression); return new expressionlambdaresult() { expressiontype = expression.nodetype, propertyname = left_name, propertyvalue = right_name }; }
/// <summary> /// get dto attribute /// </summary> private static string getleftexpression<t2>(binaryexpression expression) { var leftexpression = expression.left as memberexpression; var ps = typeof(t2).getproperties(bindingflags.declaredonly | bindingflags.public | bindingflags.nonpublic | bindingflags.static | bindingflags.instance); var first = ps.where(x => x.getcustomattribute<entitydatanameattribute>(true) != null && x.name.tolower() == leftexpression.member.name.tolower()).firstordefault(); return first != null ? first.getcustomattribute<entitydatanameattribute>(true).entitydataname : leftexpression.member.name; }
/// <summary> /// get constant value /// </summary> private static object getrightexpression(binaryexpression expression) { var rootexpression = expression.right as memberexpression; var constexpr = rootexpression.expression as constantexpression; if (constexpr == null) { object value = expression.lambda(expression.right).compile().dynamicinvoke(); if (value == null) return value = string.empty; if (getpropertytype(value.gettype()) == typeof(nullable)) { var ps = value.gettype().getproperties(bindingflags.declaredonly | bindingflags.public | bindingflags.nonpublic | bindingflags.static | bindingflags.instance); var first = ps.where(x => x.getcustomattribute<entitydatanameattribute>(true) != null).firstordefault(); if (first == null) { throw new exception(" attribute is empty "); } value = value.gettype().getproperty(first.name).getvalue(value, null); } else if (getpropertytype(value.gettype()) == typeof(listclass)) { } return value; } else { var memberinfos = new stack<memberinfo>(); memberinfos.push(rootexpression.member); object objreference = constexpr.value; var mi = memberinfos.pop(); var objfield = objreference.gettype().getfield(mi.name); return objfield.getvalue(objreference); } }
/// <summary> /// any binary operator /// </summary> /// <param name="expressiontype"></param> /// <returns></returns> private static bool anybinaryoperator(expressiontype expressiontype) { bool any = true; switch (expressiontype) { case expressiontype.andalso: break; case expressiontype.and: break; case expressiontype.orelse: break; case expressiontype.or:break; default: any = !any; break; } return any; }
public class expressionlambdaresult: expressionresult { public string propertyname { get; set; } public object propertyvalue { get; set; } } public class expressionresult { public expressiontype expressiontype { get; set; } }
上一篇: C1FlexGrid双grid滚动条联动