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 ...
随机推荐
- Nginx 安装 和 特性介绍
一:nginx 环境搭建 四项确认 确认系统网络可通行 确认yum可用 确认关闭iptables规则 确认停用selinux 查看iptables规则 iptables -L 关闭iptables规则 ...
- 四、DDL常见操作汇总
DDL: Data Define Language 数据定义语言,主要用来对数据库.表进行一些管理操作.如:建库.删库.建表.修改表.删除表.对列的增删改等. 一.库的管理 1.创建库 create ...
- 采集代理ip 地址【西刺,快代理】
# 嗯,...因为经常需要使用代理去抓一点东西,就有了下面一段代码,第一版不是很好,后面用到了再来优化 import re,pymysql,time,redis from urllib.request ...
- git相关的一篇不错的文章
原文地址:http://josh-persistence.iteye.com/blog/2215214 点击进入
- KeepAlive细谈
来自: http://blog.sina.com.cn/s/blog_e59371cc0102ux5w.html 最近工作中遇到一个问题,想把它记录下来,场景是这样的: 从上图可以看出,用户通过Cli ...
- Oracle GoldenGate(ogg)安装经验大汇总,采坑总结,绝对干货!
一下是安装ogg过程中遇到的问题和解决办法,绝对良心干货,抽空会写更详细的安装教程.更多精彩内容请点击 OGG-00685 begin time prior to oldest log in log ...
- Android笔记(十七) Android中的Service
定义和用途 Service是Android的四大组件之一,一直在后台运行,没有用户界面.Service组件通常用于为其他组件提供后台服务或者监控其他组件的运行状态,例如播放音乐.记录地理位置,监听用户 ...
- el-input maxlength 不限制长度
背景: 小鱼最近使用 input输入框时想限制输入的长度, type = "number" 时,限制的长度无效,代码如下 <el-input v-model="fo ...
- 聊聊Hash索引
hash index是基于哈希表实现的,只有精确匹配索引所有列的查询才会生效.对于每一行数据,存储引擎都会对所有的索引列计算一个hash code,并将的有的hash code存储在索引中,同时在哈希 ...
- Mybatis之foreach批量插入
1接口 public boolean insertMembersBatch(@Param("memberList") List<Members> members); @ ...