C#表达式参数解析算法,N级属性调用,函数值,变量值,常量值
标签:system 解决方案 解析算法 val var ret names const 表达式
public static object GetValue(Expression expression)
{
var names = new Stackstring>();
var exps = new Stack();
var mifs = new Stack();
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级属性调用,函数值,变量值,常量值
标签:system 解决方案 解析算法 val var ret names const 表达式
原文地址:https://www.cnblogs.com/chaeyeon/p/11162121.html
评论