private static Expression<Func<int>> ActuallyInnerAlsoCompile()
{
var strType = typeof(string);
var intType = typeof(int);
var enumearbleType = typeof(Enumerable);
var array = Expression.NewArrayInit(strType, Expression.Constant("test"), Expression.Constant("test2"));
var x = Expression.Parameter(strType, "whereParam");
var whereExp = Expression.Call(enumearbleType,
"Where",
new[] {strType},
array,
Expression.Lambda(Expression.NotEqual(Expression.PropertyOrField(x, "Length"), Expression.Constant(4)), x));
var selectExp = Expression.Call(enumearbleType,
"Select",
new[] {strType, intType},
whereExp,
Expression.Lambda(Expression.PropertyOrField(x, "Length"), x));
var firstOrDefault = Expression.Call(enumearbleType,
"FirstOrDefault",
new[] {intType},
selectExp);
return Expression.Lambda<Func<int>>(firstOrDefault);
}
answer
var lambda = ActuallyInnerAlsoCompile();
var dynamicAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName("dynamicAssembly"),
AssemblyBuilderAccess.Save);
var dm = dynamicAssembly.DefineDynamicModule("dynamicModule", "dynamic.dll");
var dt = dm.DefineType("dynamicType");
var m1 = dt.DefineMethod(
"dynamicMethod",
MethodAttributes.Public | MethodAttributes.Static);
lambda.CompileToMethod(m1);
dt.CreateType();
dynamicAssembly.Save("dynamic.dll");
// Decompiled with JetBrains decompiler
// Type: dynamicType
// Assembly: dynamicAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 94346EDD-3BCD-4EB8-BA4E-C25343918535
using System;
using System.Collections.Generic;
using System.Linq;
internal class dynamicType
{
public static int dynamicMethod()
{
return ((IEnumerable<string>) new string[2]
{
"test",
"test2"
}).Where<string>(new Func<string, bool>(dynamicType.\u003CExpressionCompilerImplementationDetails\u003E\u007B1\u007Dlambda_method)).Select<string, int>(new Func<string, int>(dynamicType.\u003CExpressionCompilerImplementationDetails\u003E\u007B2\u007Dlambda_method)).FirstOrDefault<int>();
}
private static bool \u003CExpressionCompilerImplementationDetails\u003E\u007B1\u007Dlambda_method(string whereParam)
{
return whereParam.Length != 4;
}
private static int \u003CExpressionCompilerImplementationDetails\u003E\u007B2\u007Dlambda_method(string whereParam)
{
return whereParam.Length;
}
}