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. redis 异常 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk

    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. 解决方 ...

  2. dockerfile命令说明及使用

    执行Dockerfile命令: docker build -f /path/to/a/Dockerfil Dockerfile的基本结构 Dockerfile 一般分为四部分:基础镜像信息.维护者信息 ...

  3. ARM的Semihosting技术(转)

    Semihosting技术将应用程序中的IO请求通过一定的通道传送到主机(host),由主机上的资源响应应用程序的IO请求, 而不是像在主机上执行本地应用程序一样,由应用程序所在的计算机响应应用程序I ...

  4. 关于winsxs目录的处理

    参考文档: 1.http://www.360doc.com/content/14/0415/12/7873863_369147503.shtml 2.http://www.360doc.com/con ...

  5. 优先级队列(python)

    # -*- coding:utf-8 -*- class Array(object): def __init__(self, size=32): self._size = size self._ite ...

  6. python算法与数据结构-希尔排序算法(35)

    一.希尔排序的介绍 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法. 希尔排序是把记录按下标的一定增量分组,对每 ...

  7. 04 Spring的依赖注入

    依赖注入:Dependency Injection.它是 spring 框架核心 ioc 的具体实现. 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖 ...

  8. MCMC蒙特卡罗马尔科夫模型

    https://www.cnblogs.com/pinard/p/6645766.html https://blog.csdn.net/saltriver/article/details/521949 ...

  9. Newtonsoft.Json 自定义序列化器---时间

    IsoDateTimeConverter _IsoDateTimeConverter = new IsoDateTimeConverter() { DateTimeFormat = "yyy ...

  10. C# HashCode

    如果两个对象的HashCode相等,可以认为两者相等.类型和值都相等. xx.GetHashCode();