zoukankan      html  css  js  c++  java
  • Traverse an expression tree and extract parameters

    Traverse an expression tree and extract parameters

     

    I think as you've said that using ExpressionVisitor works out to be a good approach. You don't need to implement all the Visit... methods as they already have a default implementation. From what I understood what you want is to find all property accesses of a certain type inside a lambda function

    public class MemberAccessVisitor : ExpressionVisitor
    {
        private readonly Type declaringType;
        private IList<string> propertyNames = new List<string>();
    
        public MemberAccessVisitor(Type declaringType)
        {
            this.declaringType = declaringType;
        }
    
        public IEnumerable<string> PropertyNames { get { return propertyNames; } }
    
        public override Expression Visit(Expression expr)
        {
            if (expr != null && expr.NodeType == ExpressionType.MemberAccess)
            {
                var memberExpr = (MemberExpression)expr;
                if (memberExpr.Member.DeclaringType == declaringType)
                {
                    propertyNames.Add(memberExpr.Member.Name);
                }
            }
    
            return base.Visit(expr);
        }
    }

    This could be further improved to what you want by checking the member is a property and also to get PropertyInfo rather than strings

    It could be used as follows:

    var visitor = new MemberAccessVisitor(typeof(TSource));
    
    visitor.Visit(memberMap);
    
    var propertyNames = visitor.PropertyNames;
  • 相关阅读:
    Android一些问题
    内存泄漏(Memory Leak)
    内存溢出OOM
    Android面试题集合
    Handler之同步屏障机制(sync barrier)
    IdleHandler 原理浅析
    OkHttp原理
    RxJava操作符
    Android电量优化全解析
    Android内存优化大盘点
  • 原文地址:https://www.cnblogs.com/chucklu/p/11580209.html
Copyright © 2011-2022 走看看