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 基 ...
随机推荐
- 如何解决Eclipse启动时画面一闪而过
以前Eclipse都可以正常使用,突然有一天不能启动了,点击图标后启动画面一闪之后就消失了,以下是一些解决方案 1. 找到Eclipse目录下的eclipse.exe,右键点击->发送到桌面快捷 ...
- cas单点注销失败Error Sending message to url endpoint
最近在做cas单点登录时,由于是单点登录.必然会涉及到单点注销,然而在做单点注销时由于对cas注销机制不了解加之测试条件所致,所有测试都是在本机下完成(机器性能较低,没用虚拟机):导致折腾了很久.网上 ...
- swift 中delegate的使用
今天写了delegate,遇到以下问题: 这里protocol的写法有问题,如果delegate指向一个实现了某个协议对象的引用,在oc里是这样写delegate的类型 id<protocol& ...
- iOS 端的第三方语音识别库
最近在看语音识别方面的库,主要看了2个收费的项目,一个是 At&t 的,一个是Nuance的.这2个项目虽然是收费的,但是仅仅测试的话,是免费的,连接如下 https://developer. ...
- 【回溯】图的m着色问题
问题 C: [回溯]图的m着色问题 时间限制: 1 Sec 内存限制: 128 MB提交: 1 解决: 1[提交][状态][讨论版] 题目描述 给定无向连通图G=(V, E)和m种不同的颜色,用这 ...
- java\c程序的内存分配
JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的任何Java代码能够在该 ...
- frameset框架下,刷新整个页面
<a href="index.jsp" target="_parent"> index.jsp主frameset页面
- HDU1267 递推
下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Hudson可扩展持续集成引擎
参考文章:http://blog.csdn.net/dazhi_100/article/details/11629133 极限编程中一项建议实践便是持续集成,持续集成是指在开发阶段,对项目进行持续性自 ...
- cf50A(水题)
题意:m*n的地板最多能铺多少2*1的地板砖,不能重复... 水题.. 上代码... #include <iostream> #include <stdio.h> using ...