Evaluate string expressions that test object properties without recompiling for every object?

时间:2016-05-17 11:06:30

标签: c# eval

There are a few tools out there that let you dynamically evaluate expressions written in C# code, similar to JavaScript's eval(). One example is https://csharpeval.codeplex.com I am sure there are others.

These tend to do 1-time evaluation of the expression and return a function/delegate you can run many times - but will they allow the same expression to be run on multiple data?

e.g:

class MyClass
{
 public int Prop1{get}
 public int Prop2{get}
 public int Prop3{get}
}

string Expression = "(x.Prop1 == 2 && x.Prop2 >3) || x.Prop3 < 99";

IList<MyClass> objects = ...;

FindAllTheObjectsThatMatch(objects, Expression);

Clearly evaluating Expression for each object is Not Good. Is this achievable?

Going further, can I even use these things in LINQ i.e. pseudo code like objects.Where(Expression)

0 个答案:

没有答案