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

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

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

  3. 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现

    前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...

  4. 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. 5.Primitive, Reference, and Value Types

    1.Programming Language Primitive Types primitive types:Any data types the compiler directly supports ...

  6. SignalR Troubleshooting

    This document contains the following sections. Calling methods between the client and server silentl ...

  7. C# 语言规范_版本5.0 (第4章 类型)

    1. 类型 C# 语言的类型划分为两大类:值类型 (Value type) 和引用类型 (reference type).值类型和引用类型都可以为泛型类型 (generic type),泛型类型采用一 ...

  8. QML Object Attributes QML对象属性

    QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...

  9. 新书《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    基 ...

随机推荐

  1. PHP 逻辑思维题

    约瑟夫环 一群猴子排成一圈,按1,2,...,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去...,如此不停的进行下去,直到最后只剩下一只猴子为止 ...

  2. django inclusion_tag

    一种比较普遍的tag类型是只是渲染其它模块显示下内容,这样的类型叫做Inclusion Tag. 例如,实现以下tag: {% books_for_author author %} 渲染结果为: &l ...

  3. Django对静态文件的处理——部署阶段

    参考:http://blog.makto.me/post/2012-11-09/static-files-in-django-deployment HTML模板中的用法: {% load static ...

  4. Http 协议Header

    Responses 部分 Header 解释 示例 Accept-Ranges 表明服务器是否支持指定范围请求及哪种类型的分段请求 Accept-Ranges: bytes Age 从原始服务器到代理 ...

  5. java代码获取jdbc链接properties

    public static String getDirPath() { Resource resource = null; Properties props = null; String driver ...

  6. Redhat修改主机名_Red hat怎么永久修改主机名hostname(转)

    有几种方式修改Redhat的主机名字,这些方法也适合其他的Centos系统,下面介绍Red hat怎么永久修改主机名hostname的三种方法. 方法一: 说明"hostname" ...

  7. 好久没弄了,来个最简的centos下的Iptables文件存照吧。

    有最基本的作用.. # Generated by iptables-save v1. :: *filter :INPUT ACCEPT [:] :FORWARD ACCEPT [:] :OUTPUT ...

  8. Xamarin.Android开发实践(十二)

    Xamarin.Android之ContentProvider 一.前言 掌握了如何使用SQLiteOpenHelper之后,我们就可以进行下一步的学习.本章我们将会学习如何使用ContentProv ...

  9. SQLite使用方法 SQLiteOpenHelper操作(转)

    SQLiteOpenHelper主要用于 创建数据库 SQLiteDatabase 主要用于 执行sql语句 程序内使用SQLite数据库是通过SQLiteOpenHelper进行操作 1.      ...

  10. 【转】cloudera新增用户权限配置

    转自 http://lookqlp.iteye.com/blog/2189119  .   配置起来较复杂,需要在有测试环境之后再进行配置测试.  之后是有上HUE的计划的,所以这个也是一定要做的. ...