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;

Traverse an expression tree and extract parameters的更多相关文章

  1. 表达式树(Expression Tree)

    饮水思源 本文并非原创而是下面网址的一个学习笔记 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/e ...

  2. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  3. Expression Tree 扩展MVC中的 HtmlHelper 和 UrlHelper

    表达式树是LINQ To everything 的基础,同时各种类库的Fluent API也 大量使用了Expression Tree.还记得我在不懂expression tree时,各种眼花缭乱的A ...

  4. 转载Expression Tree揭秘

    概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...

  5. 深入学习C#匿名函数、委托、Lambda表达式、表达式树类型——Expression tree types

    匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...

  6. [转]打造自己的LINQ Provider(上):Expression Tree揭秘

    概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...

  7. 打造自己的LINQ Provider(上):Expression Tree揭秘

    概述 在.NET Framework 3.5中提供了LINQ 支持后,LINQ就以其强大而优雅的编程方式赢得了开发人员的喜爱,而各种LINQ Provider更是满天飞,如LINQ to NHiber ...

  8. Expression Tree上手指南 (一)

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

  9. Expression Tree 学习笔记(一)

    大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...

随机推荐

  1. 关于Tomcat服务器中的协议及请求过程

    关于Tomcat服务器中采用的协议:在Tomcat的server.xml文件中可以找到如下几个Connector <!-- 1. HTTP --> <Connector port=& ...

  2. mysql基础指令知识

    桌面指令(cmd)进入mysql客户端 第一步:安装mysql,配置环境变量 第二步:手动开启服务 第三步:输入如下指令: mysql [-h localhost -P 3306] -u  用户名 - ...

  3. @ConfigurationProperties注解和@Value注解的区别

    都是读取配置文件属性 1.  @ConfigurationProperties(prefix = "person")读取多个属性 @Component @Configuration ...

  4. python之变量的数据类型(3)dict 及解构简单介绍

    一.变量的数据类型(3) 1. dict 字典dict 用{}来表示 键值对数据 {key:value} 唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 2.增删 ...

  5. thinkphp概述2

    thinkphp概述,thinkphp项目构建流程,thinkphp项目结构,thinkphp配置,thinkphp控制器,thinkphp模型,thinkphp视图,thinkphp的内置模板引擎. ...

  6. request-html 登陆百度

    import asyncio from requests_html import HTMLSession url = 'https://passport.baidu.com/v2/?login& ...

  7. 检查SQL Server数据库各个库表空间使用的方法

    /*创建一张表:表名Data,列名:表名,列数,预留空间,数据占用空间,索引占用空间,剩余空间*/ CREATE TABLE Data ( 表名 ), 列数 ), 预留空间 ), 数据占用空间 ), ...

  8. P2756 飞行员配对方案问题[二分图最大匹配]

    题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中,每一名 ...

  9. Nmap扫描工具实验报告

    实验报告 实验内容 通过ping进行操作系统探测 利用Zenmap/Nmap进行TCP connet扫描.TCP SYN扫描和操作系统扫描 实验目的 了解扫描的一般步骤 熟练使用ping命令并能够进行 ...

  10. oracle数据库锁表

    在团队开发一个项目的时候,避免不了两个或两个以上的人同时操作某一数据库中的同一张表,这时候,如果一个用户没有提交事务,或者忘记提交事务,那么其他用户就不能对这张表进行操作了,这是很烦人的事情,下面是查 ...