且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c#将字符串表达式转换为布尔表达式

更新时间:2023-02-18 10:46:45

我会使用 Irony,.NET 语言工具包.您可以使用 Irony 构建一个简单的语法,然后将字符串解析为可执行命令.在这个教程Expression Grammar Sample,这是一个很常见的请求;)

I would use Irony, the .NET language kit. You could construct a simple grammar with Irony and then parse the string into executable command. There's a decent example of an arthmetic grammar in this tutorial and in the Expression Grammar Sample, its a pretty common request ;)

我绝对建议使用适当的编译器,而不是 Regex 或使用自己的方法 - 如果您想添加更多规则,它会更具可扩展性.

I definitely suggest using a proper compiler as opposed to Regex or a roll your own approach - it will be much more extensible if you ever want to add more rules.