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 ...
随机推荐
- gcc 编译的四大过程
gcc 编译的四大过程(预处理-编译-汇编-链接 ) 我们来编译一个hello world 程序. #include <stdio.h> int main(int argc,const c ...
- [LeetCode] 342. 4的幂 ☆(是否4 的幂)
描述 给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方. 示例 1: 输入: 16输出: true示例 2: 输入: 5输出: false 进阶:你能不使用循环或者递归 ...
- python实用库
参考:https://github.com/programthink/opensource/blob/master/libs/python.wiki#35_ Python 开源库及示例代码 Table ...
- 前端框架开始学习Vue(一)
MVVM开发思想图(图片可能会被缩小,请右键另存查看,图片来源于网络) 定义基本Vue代码结构 1 v-text,v-cloak,v-html命令 默认 v-text没有闪烁问题,但是会覆盖元 ...
- 【leetcode】280.Wiggle Sort
原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...
- DoraBox漏洞测试环境搭建和测试过程
未完待续...
- IOTA私有链简单搭建
IOTA 参考:https://github.com/iotaledger/wallet 参考:https://github.com/iotaledger/iota.js 参考:https://git ...
- zookeeper入门(1)---基本概念
转载 : https://blog.csdn.net/java_66666/article/details/81015302 一. zookeeper概念 它是一个分布式服务框架,是Apache Ha ...
- Oracle使用游标查询所有数据表备注
功能作用:应用对应的SQL语句,能方便快速的查询Oracle数据库指定用户的所有用户表说明,快速知道每个数据表是做什么的,方便写文档和方案. 运行环境:搭建好Oracle数据库,并使用PQ/SQL D ...
- puppeteer报错 UnhandledPromiseRejectionWarning: Error: Protocol error (Page.getLayoutMetrics): Target closed.
puppeteer运行时报错: UnhandledPromiseRejectionWarning: Error: Protocol error (Page.getLayoutMetrics): Tar ...