Traverse an expression tree and extract parameters
2024-08-26 11:37:10
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;
最新文章
- 高性能网站架构设计之缓存篇(1)- Redis的安装与使用
- NBUT 1010 魔法少女(DP)
- 帮初学者改代码——playerc之“练习:求完数问题”(下)
- E2 2014.08.05 更新日志
- 集合框架学习之Guava Collection
- ubuntu find方法
- 重写OnPaint事件对窗体重绘(显示gif动画) 实例2
- 数据泵导出/导入Expdp/impdp
- 浏览器 HTTP 缓存原理分析
- echarts 某省下钻某市地图
- JSP慕课网阶段用户登录小例子(不用数据库)
- 基于MySQL + Node.js + Leaflet的离线地图展示,支持百度、谷歌、高德、腾讯地图
- Matplotlib初体验
- The MySQL Server
- Oracle11g温习-第十六章:用户管理
- docker 简单入门(一)
- [深入浅出iOS库]之图形库CorePlot
- 安卓开发之不通过USB数据线调试的方法
- Android逆向之旅---静态方式分析破解视频编辑应用「Vue」水印问题
- 英雄pk理解面向对象中的this指针概念
热门文章
- 如何在SAP Cloud Platform ABAP编程环境里创建一个employee
- MySQL数据库的事物隔离级别
- Redhat下Oracle 12c单节点安装
- nginx简单学习
- 【Low版】HAUT - OJ - Contest1035 - 2017届新生周赛(六)题解
- 使用Restful风格中的post使用过遇到前端数据传送不到后端
- Union-Find(并查集): Dynamic Connectivity 问题
- C# 4.0 新特性(.NET Framework 4.0 与 Visual Studio 2010 )
- EPL II 编程打印
- 通过命令行运行java出现";错误: 找不到或无法加载主类 ";解决办法