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

尝试Lambda表达式公共操作类(二)

  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的更多相关文章

  1. 动态拼接linq 使用Expression构造动态linq语句

    最近在做动态构造linq语句,从网上找了很多,大多数,都是基于一张表中的某一个字段,这样的结果,从网上可以搜到很多.但如果有外键表,需要动态构造外键表中的字段,那么问题来了,学挖掘机哪家强?哦,不是, ...

  2. access 2007 vba 开发中学到的知识(三)

    打开文件或程序 'API函数声明Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellEx ...

  3. 表达式树(Expression Tree)

    你每创建一个表示表达式的实例时,都可以将该类型实例看成是一棵表达式树.每种表示表达式的类型都有一个具体的类型,如Expression的Variable()方法创建的是ParameterExpressi ...

  4. [C++] OOP - Access Control and Class Scope

    Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...

  5. 使用Expression动态创建lambda表达式

    using System;using System.Linq.Expressions;using System.Reflection; namespace Helper{ public class L ...

  6. Expression表达式目录树

    一.初识Expression 1.在上一篇我们讲到了委托(忘记了可以在看看,点赞在看养成习惯),今天要讲的Expression也和委托有一点点关系吧(没有直接关系,只是想要大家看看我其他的文章),Ex ...

  7. 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 ...

  8. Expression 表达式动态生成

    http://blog.csdn.net/duan1311/article/details/51769119 以上是拼装和调用GroupBy的方法,是不是很简单,只要传入分组列与合计列就OK了! 下面 ...

  9. Object lifetime

    Object lifetime Temporary object lifetime Storage reuse Access outside of lifetime Every object has ...

随机推荐

  1. node.js 微信开发1-接入

    准备工作1 域名准备 无论是个人开发还是做公司项目域名都是必不可少的 前期我个人用过花生壳做个开发测试,挺好用的,就是现在要收费了,开通花生壳要收费,开通内网穿透要收费(为啥要内网穿透呢,因为微信接入 ...

  2. nginx 之高级模块

    secure_link_module 模块 作用: 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问 限制链接生效周期 配置语法 Syntax:secure_link expressio ...

  3. Vue注意事项

    在使用Vue中的函数或自己定义的函数或指令的时候,Vue说明如下 在一些自己定义或系统定义的驼峰命名规则的时候,你需要到元素区域引用的使用中间的大写要改成小写在谭家 一条横杠如: 你在var=new ...

  4. redis过期事件回调函数,与有序集合

    https://cloud.tencent.com/developer/article/1347437  python中的Redis键空间通知(过期回调) set notify-keyspace-ev ...

  5. ubuntu18.04 为应用程序添加桌面图标

    一.桌面图标位置 Lniux下桌面图标储存路径为:/usr/share/applications 二.桌面图标格式 所有桌面图标格式均为desktop,即名为XXX.desktop 三.编辑内容(常用 ...

  6. shiro系列五、shiro密码MD5加密

    Shiro-密码的MD5加密   1.密码的加密 在数据表中存的密码不应该是123456,而应该是123456加密之后的字符串,而且还要求这个加密算法是不可逆的,即由加密后的字符串不能反推回来原来的密 ...

  7. 分布式Hadoop和HBase整合

    环境: CentOS 6.6                hadoop-2.6.0            hbase-0.98.10.1 HBase1    192.168.1.4     Mast ...

  8. Linux命令——lsb_release

    简介 LSB是Linux Standard Base(Linux标准库)的缩写, lsb_release命令 用来与具体Linux发行版相关的Linux标准库信息. 注:LSB的译法有Linux标准库 ...

  9. 新闻系统——SQLHelper助手优化

    在前面我们机房重构的时候已经用过了SQLHelper,但当时就是只会应用,知道利用SQLHelper能够帮助我们连接数据库,但对于怎样书写还是不太清楚,今天就揭开这层神秘的面纱,来真正体验如何来编写S ...

  10. 7月新的开始 - Axure学习06 - 母版的使用

    母版的使用 主导航.底部.在很多页面上都是一样的: 如果在每一个页面都写一次的化.话.是非常浪费时间的,为了方便.可以使用母版: 母版可以帮助我们将一些元素重复利用,既可以保证页面的统一性.还可以节省 ...