eval(string)转换为C#代码

问题描述:

是否可以在运行时使用C#评估以下内容

Is it possible to evaluate the following in C# at runtime

我有一个包含3个属性的类( Field 运算符

I have a class that contains 3 properties (Field,Operator,Value)

 rule.Field;
 rule.Operator;
 rule.Value;

这是我的规则类...

this is my rule class...

现在我有一个循环

foreach(item in items)
   {
       // here I want to create a dynamic expression to evaluate at runtime
       // something like
       if (item.[rule.field] [rule.operator] [rule.value])
           { do work }
   }

我只是不知道语法,或者如果在C#中可能,我知道在JS中这是可能的,但这不是编译语言。

I just don't know the syntax, or if its possible in C#, I know in JS its possible but that's not a compiled language.

更新

基本上,我想要一种的方法eval(stringCode)或更好的受支持的方法。

Essentially I want a way to eval(stringCode) or a better more supported way.

我不完全确定你是说。您可以尝试澄清一下吗?

I'm not entirely sure what you are saying. Can you try clarifying it a bit?

是否要获取字符串表达式并在C#中运行时对其求值?如果是这样,答案是否定的。 C#不支持此类动态评估。

Are you wanting to to take a string expression and evaluate it at runtime in C#? If so the answer is no. C# does not support such types of dynamic evaluation.