C#表达式参数解析算法,N级属性调用,函数值,变量值,常量值
Posted chaeyeon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#表达式参数解析算法,N级属性调用,函数值,变量值,常量值相关的知识,希望对你有一定的参考价值。
public static object GetValue(Expression expression) var names = new Stack<string>(); var exps = new Stack<Expression>(); var mifs = new Stack<System.Reflection.MemberInfo>(); var tempExpression = expression; while (tempExpression is MemberExpression) var member = tempExpression as MemberExpression; names.Push(member.Member.Name); exps.Push(member.Expression); mifs.Push(member.Member); tempExpression = member.Expression; if (names.Count > 0) object value = null; foreach (var name in names) var exp = exps.Pop(); var mif = mifs.Pop(); if (exp is ConstantExpression cex) value = cex.Value; if ((mif is System.Reflection.PropertyInfo pif)) value = pif.GetValue(value); else if ((mif is System.Reflection.FieldInfo fif)) value = fif.GetValue(value); return value; else if (expression is ConstantExpression) return (tempExpression as ConstantExpression).Value; else return Expression.Lambda(expression).Compile().DynamicInvoke();
完整解决方案:https://www.cnblogs.com/chaeyeon/p/11028480.html
以上是关于C#表达式参数解析算法,N级属性调用,函数值,变量值,常量值的主要内容,如果未能解决你的问题,请参考以下文章