Access the value of a member expression
Access the value of a member expression
解答1
You can compile and invoke a lambda expression whose body is the member access:
private object GetValue(MemberExpression member)
{
var objectMember = Expression.Convert(member, typeof(object));
var getterLambda = Expression.Lambda<Func<object>>(objectMember);
var getter = getterLambda.Compile();
return getter();
}
Local evaluation is a common technique when parsing expression trees. LINQ to SQL does this exact thing in quite a few places.
解答2
MemberExpression right = (MemberExpression)((BinaryExpression)p.Body).Right;
Expression.Lambda(right).Compile().DynamicInvoke();
会有同样的问题
尝试gist
https://gist.github.com/jcansdale/3d4b860188723ea346621b1c51fd8461#file-expressionutilities-cs-L13
UKERecognition.Infrastructure.Exceptions.UIException: UserInterface ---> System.Exception: Couldn't evaluate Expression without compiling: l
private static object GetMemberValue(MemberExpression expression)
{
if (expression == null)
return null;
var field = expression.Member as FieldInfo;
if (field != null)
{
var constValue = GetConstantValue(expression.Expression);
return field.GetValue(constValue);
}
var property = expression.Member as PropertyInfo;
if (property == null)
return null;
var value = GetMemberValue(expression.Expression as MemberExpression);
return property.GetValue(value);
} private static object GetConstantValue(Expression expression)
{
var constantExpression = expression as ConstantExpression;
if (constantExpression == null)
return null;
return constantExpression.Value;
}
System.Reflection.TargetException: Non-static method requires a target.
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at LISA.Custom.Infrastructure.LisaExpressionVisitor`1.GetMemberValue(MemberExpression expression) in
Access the value of a member expression的更多相关文章
- 动态拼接linq 使用Expression构造动态linq语句
最近在做动态构造linq语句,从网上找了很多,大多数,都是基于一张表中的某一个字段,这样的结果,从网上可以搜到很多.但如果有外键表,需要动态构造外键表中的字段,那么问题来了,学挖掘机哪家强?哦,不是, ...
- access 2007 vba 开发中学到的知识(三)
打开文件或程序 'API函数声明Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellEx ...
- 表达式树(Expression Tree)
你每创建一个表示表达式的实例时,都可以将该类型实例看成是一棵表达式树.每种表示表达式的类型都有一个具体的类型,如Expression的Variable()方法创建的是ParameterExpressi ...
- [C++] OOP - Access Control and Class Scope
Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...
- 使用Expression动态创建lambda表达式
using System;using System.Linq.Expressions;using System.Reflection; namespace Helper{ public class L ...
- Expression表达式目录树
一.初识Expression 1.在上一篇我们讲到了委托(忘记了可以在看看,点赞在看养成习惯),今天要讲的Expression也和委托有一点点关系吧(没有直接关系,只是想要大家看看我其他的文章),Ex ...
- Building Applications with Force.com and VisualForce(Dev401)( 八):Designing Applications for Multiple Users: Controling Access to Records.
Module Objectives1.List feature that affect access to data at the record level.2.List the organizati ...
- Expression 表达式动态生成
http://blog.csdn.net/duan1311/article/details/51769119 以上是拼装和调用GroupBy的方法,是不是很简单,只要传入分组列与合计列就OK了! 下面 ...
- Object lifetime
Object lifetime Temporary object lifetime Storage reuse Access outside of lifetime Every object has ...
随机推荐
- mongodb副本集和分片存储理论整理
目录 理论概述 一.各种集群简述 二.原理 主从复制 Mongodb副本集 理论概述 一.各种集群简述 mongodb有三种集群搭建方式: 分片:sharding.指为处理大量数据,将数据分开存储,不 ...
- shiro系列五、shiro密码MD5加密
Shiro-密码的MD5加密 1.密码的加密 在数据表中存的密码不应该是123456,而应该是123456加密之后的字符串,而且还要求这个加密算法是不可逆的,即由加密后的字符串不能反推回来原来的密 ...
- Linux服务器集群代理配置
因为之前本科参与开发的一个互联网新闻采集系统需要爬取几个国外的新闻站点,通过翻墙才能访问,而我们的服务器是阿里云服务器,没有操作界面,而且抽取任务是定时执行,必须要实现程序控制VPN的连接与断开.所以 ...
- 版本问题---Bazel与tensorflow的对应关系
源码安装tf的时候,会用到Bazel,版本不对应,后面会引起好多麻烦. echo "deb [arch=amd64] http://storage.googleapis.com/bazel- ...
- Codeforces #496 E1. Median on Segments (Permutations Edition)
http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...
- 使用poi进行数据的导出Demo
这是本人在项目中遇到了一个导出数据时,如果该条数据中包含汉字,就会出现excel单元格的大小与期望的样式不一样,也是查找了半天,也没有发现哪里出的问题. 现将一个小Demo奉献在这里,可以在遇到使用p ...
- vue 配置 TinyMCE
1.index.html 增加cdn 地址 <script src="//cdn.bootcss.com/tinymce/5.0.16/tinymce.min.js"> ...
- 53、servlet3.0-简介&测试
53.servlet3.0-简介&测试 Servlet 4.0 : https://www.jcp.org/en/jsr/summary?id=servlet+4.0
- mongodb 导入json文件遇到的坑
使用mongoimport命令导入外部json文件时,发现一直报错 报错结果如下: json数据格式完全正确如下: 经过再三确认格式最终找到解决方案,原来用cmd导入数据时json { }包含的数据 ...
- Greenplum 调优--数据倾斜排查(一)
对于分布式数据库来说,QUERY的运行效率取决于最慢的那个节点. 当数据出现倾斜时,某些节点的运算量可能比其他节点大.除了带来运行慢的问题,还有其他的问题,例如导致OOM,或者DISK FULL等问题 ...