eval(string) to C# code

后端 未结 8 1957
傲寒
傲寒 2021-01-19 04:23

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

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

8条回答
  •  爱一瞬间的悲伤
    2021-01-19 04:46

    While it is true that you probably won't find an elegant way to evaluate full C# code on the fly without the use of dynamically compiling code (which is never pretty), you can almost certainly get your rules evaluated in short order using either the DLR (IronPython, IronRuby, etc) or an expression evaluator library that parses and executes a custom syntax. There is one, Script.NET, that provides a very similar syntax to C#.

    Take a look here:Evaluating Expressions a Runtime in .NET(C#)

    If you have the time / inclination to learn a little Python, then IronPython and the DLR will solve all your issues: Extending your App with IronPython

提交回复
热议问题