Here are the examples of the csharp api System.Linq.Expressions.Expression.Compile(bool) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
6 Examples
19
Source : Engine.cs
with GNU Affero General Public License v3.0
from ankenyr
with GNU Affero General Public License v3.0
from ankenyr
public static Func<T, bool> CompileRule<T>(Expression r)
{
var paramUser = System.Linq.Expressions.Expression.Parameter(typeof(Operand));
System.Linq.Expressions.Expression expr = BuildExpr<T>(r, paramUser);
// build a lambda function User->bool and compile it
var value = System.Linq.Expressions.Expression.Lambda<Func<T, bool>>(expr, paramUser).Compile(true);
return value;
}
19
Source : Sum.cs
with BSD 3-Clause "New" or "Revised" License
from exomia
with BSD 3-Clause "New" or "Revised" License
from exomia
public static Func<T[], T> CreateSumFunc()
{
ParameterExpression arrayExpr = Expression.Parameter(typeof(T[]));
ParameterExpression iExpr = Expression.Variable(typeof(int));
ParameterExpression rExpr = Expression.Variable(typeof(T));
LabelTarget breakLabel = Expression.Label(typeof(T));
return Expression.Lambda<Func<T[], T>>(
Expression.Block(
new[] { rExpr, iExpr },
Expression.Loop(
Expression.IfThenElse(
Expression.LessThan(iExpr, Expression.ArrayLength(arrayExpr)),
Expression.Block(
Expression.replacedign(
rExpr,
Expression.Add(rExpr, Expression.ArrayAccess(arrayExpr, iExpr))),
Expression.replacedign(iExpr, Expression.Increment(iExpr))),
Expression.Break(breakLabel, rExpr)
),
breakLabel)), arrayExpr)
.Compile(false);
}
19
Source : Sum.cs
with BSD 3-Clause "New" or "Revised" License
from exomia
with BSD 3-Clause "New" or "Revised" License
from exomia
private static Func<IEnumerable<T>, T> __sum()
{
Type elementType = typeof(T);
Type enumerableType = typeof(IEnumerable<>).MakeGenericType(elementType);
Type enumeratorType = typeof(IEnumerator<>).MakeGenericType(elementType);
ParameterExpression arrayExpr = Expression.Parameter(typeof(IEnumerable<T>));
ParameterExpression rExpr = Expression.Variable(elementType, "result");
ParameterExpression enumeratorVar = Expression.Variable(enumeratorType);
MethodCallExpression moveNextCall = Expression.Call(
enumeratorVar, typeof(IEnumerator).GetMethod("MoveNext") ?? throw new InvalidOperationException());
LabelTarget breakLabel = Expression.Label(elementType);
return Expression.Lambda<Func<IEnumerable<T>, T>>(
Expression.Block(
new[] { enumeratorVar, rExpr },
Expression.replacedign(
enumeratorVar,
Expression.Call(
arrayExpr,
enumerableType.GetMethod("GetEnumerator") ??
throw new InvalidOperationException())),
Expression.Loop(
Expression.IfThenElse(
Expression.Equal(moveNextCall, Expression.Constant(true)),
Expression.replacedign(
rExpr,
Expression.Add(rExpr, Expression.Property(enumeratorVar, "Current"))),
Expression.Break(breakLabel, rExpr)
),
breakLabel)), arrayExpr)
.Compile(false);
}
19
Source : Program.cs
with MIT License
from reaqtive
with MIT License
from reaqtive
private static void Test<T>(Expression<T> expression, Func<T, object> invoke) where T : Delegate
{
Console.WriteLine("================================================================================");
Console.WriteLine(expression);
Console.WriteLine("--------------------------------------------------------------------------------");
PrintDebugView(expression);
Printreplacedle("Regular - Compiled", ConsoleColor.Red);
PrintIL(expression);
Console.WriteLine(invoke(expression.Compile()));
Console.WriteLine();
Printreplacedle("Regular - Interpreted", ConsoleColor.Red);
PrintInterpreterInstructions(expression);
Console.WriteLine(invoke(expression.Compile(preferInterpretation: true)));
Console.WriteLine();
Printreplacedle("JIT - Compiled", ConsoleColor.Red);
var d1 = expression.Compile(CompilationOptions.EnableJustInTimeCompilation);
PrintJitInfo(d1);
Console.WriteLine(invoke(d1));
Console.WriteLine();
Printreplacedle("JIT - Interpreted", ConsoleColor.Red);
var d2 = expression.Compile(CompilationOptions.EnableJustInTimeCompilation | CompilationOptions.PreferInterpretation);
PrintJitInfo(d2, preferInterpretation: true);
Console.WriteLine(invoke(d2));
Console.WriteLine();
Console.WriteLine("================================================================================");
Console.WriteLine();
}
19
Source : LambdaExpressionExtensions.cs
with MIT License
from reaqtive
with MIT License
from reaqtive
public static TDelegate Compile<TDelegate>(this Expression<TDelegate> expression, CompilationOptions options)
{
if (expression == null)
throw new ArgumentNullException(nameof(expression));
expression = OptimizeIfEnabled(expression, options);
if ((options & CompilationOptions.EnableJustInTimeCompilation) != 0)
{
return (TDelegate)(object)JitCompile(expression, options);
}
if ((options & CompilationOptions.PreferInterpretation) != 0)
{
return expression.Compile(preferInterpretation: true);
}
return expression.Compile();
}
19
Source : EqualityBinding.cs
with Apache License 2.0
from SIDOVSKY
with Apache License 2.0
from SIDOVSKY
private protected Func<T?> BuildGetter(Expression expression)
{
return _interpreter.TryBuildGetter<T>(expression)
?? Lambda<Func<T?>>(expression).Compile(true);
}