Dynamic Predicates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using DynamicLinq | |
public static Func<TInput, TOutput> GetFuncFromLinq<TInput, TOutput>(this string predicate) | |
{ | |
if (string.IsNullOrWhiteSpace(predicate)) return null; | |
LambdaExpression expression = DynamicExpression.ParseLambda(typeof(TInput), typeof(TOutput), predicate); | |
Func<TInput, TOutput> func = x => (TOutput)expression.Compile().DynamicInvoke(x); | |
return func; | |
} | |
// using CS-Script | |
public static Func<dynamic, bool> GetFuncFromScript(this string predicate) | |
{ | |
if (string.IsNullOrWhiteSpace(predicate)) return null; | |
string scriptText = @"bool GetPredicate(dynamic e) { return " + predicate + "; }"; | |
Func<dynamic, bool> result = CSScript.Evaluator.LoadDelegate<Func<dynamic, bool>>(scriptText); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment