Dynamic Expression.Call Any
public class Foo
{
public IList<string> Strings { get; set; }
} class Program
{
static void Main(string[] args)
{
//Func<Foo, bool> func =
// a => a.Strings.Any(b => b == "asdf"); // b => b == "asdf";
var bParameter = Expression.Parameter(typeof(string), "b");
var asdfConstant = Expression.Constant("asdf");
var compare = Expression.Equal(bParameter, asdfConstant);
var compareExpression = Expression.Lambda<Func<string, bool>>(compare, bParameter);
var ceCompareExpression = Expression.Constant(compareExpression.Compile()); // a => a.Strings.Any(compareExpression)
var parameter = Expression.Parameter(typeof(Foo), "foo"); var foosProperty = Expression.Property(parameter, typeof(Foo).GetProperty("Strings"));
MethodInfo method = typeof(Enumerable).GetMethods().Where(m => m.Name == "Any" && m.GetParameters().Length == ).Single().MakeGenericMethod(typeof(string)); var anyMethod = Expression.Call(method, foosProperty, ceCompareExpression); var lambdaExpression = Expression.Lambda<Func<Foo, bool>>(anyMethod, parameter); // Test.
var foo = new Foo { Strings = new List<string> { "asdf", "fdsas" } }; //Console.WriteLine(string.Format("original func result: {0}", func(foo)));
Console.Write(string.Format("constructed func result: {0}", lambdaExpression.Compile()(foo))); Console.ReadKey();
}
}
Dynamic Expression.Call Any的更多相关文章
- "One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?"的解决方法
#事故现场: 在一个.net 4.0 的项目中使用dynamic,示例代码如下: private static void Main(string[] args) { dynamic obj; obj ...
- One or more types required to compile a dynamic expression cannot be found.
This is because dynamic keyword is a new C# keyword. So we need to import Microsoft.CSharp.dll. Here ...
- 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现
前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...
- CLR via C# 3rd - 05 - Primitive, Reference, and Value Types
1. Primitive Types Any data types the compiler directly supports are called primitive types. ...
- 5.Primitive, Reference, and Value Types
1.Programming Language Primitive Types primitive types:Any data types the compiler directly supports ...
- SignalR Troubleshooting
This document contains the following sections. Calling methods between the client and server silentl ...
- C# 语言规范_版本5.0 (第4章 类型)
1. 类型 C# 语言的类型划分为两大类:值类型 (Value type) 和引用类型 (reference type).值类型和引用类型都可以为泛型类型 (generic type),泛型类型采用一 ...
- QML Object Attributes QML对象属性
QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...
- 新书《Ext JS 4.2实战》即将出版
目录: 第1章 Ext JS 4概述1.1 从Ext JS 4.0到4.071.2 从4.1到4.1.1a1.3 从4.2到4.2.11.4 如何选择版本1.5 基 ...
随机推荐
- jekyll中文乱码问题
一般编写都是采用utf-8的吧,但是在windows下安装的jekyll,默认是以GBK编码的方式去读取咱们编写的文件,如此便乱码了. 要解决此问题,总不至于要写GBK编码的文件吧,毕竟这个编码不怎么 ...
- JAVA 中BIO,NIO,AIO的理解
[转自]http://qindongliang.iteye.com/blog/2018539 ?????????????????????在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解 ...
- JS Replace 全部替换字符 用法
转载自:http://www.cnblogs.com/skykang/archive/2011/08/04/2127158.html <script language="javascr ...
- VirtualBox 虚拟 Ubuntu 的一些感想
之前有说过最近在学习Shell,公司及家里的电脑目前都是Windows系统,进行shell学习主要是用一些模拟linux环境的软件,比如banbun.cygwin banbun的功能很强大,集成了oh ...
- linux下文件压缩与解压操作
对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可 ...
- Maximum sum(poj 2479)
题意:给一段数列,将这个数列分成两部分,使两部分的最大子段和的和最大,输出和 /* 看数据没想到是(O)n的算法,求出从前向后的最大子段和和从后向前的最大子段和, 然后枚举断点. 第一次提交不小心折在 ...
- stm32——Flash读写
stm32——Flash读写 一.Flash简介 通过对stm32内部的flash的读写可以实现对stm32的编程操作. stm32的内置可编程Flash在许多场合具有十分重要的意义.如其支持ICP( ...
- mysql 超级管理员
mysql> grant all privileges on *.* to 'master'@'%' identified by '3306' with grant option; Query ...
- Java配置环境变量、方法和原因
首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入“计算机 ...
- JFrame类 和 JOptionPane类
import javax.swing.JFrame; import javax.swing.JOptionPane; public class Ch2Sample1 { public static v ...