Is there an eval function In C#?

前端 未结 7 1852
[愿得一人]
[愿得一人] 2020-12-05 03:41

I\'m typing an equation into a TextBox that will generate the graph of the given parabola. Is it possible to use an eval function? I\'m using C# 2010 and it doesn\'t have Mi

相关标签:
7条回答
  • 2020-12-05 04:16

    It's possible using the System.Reflection.Emit or System.CodeDom namespaces, but it's not exactly a good idea as there's no mechanism to control what namespaces are and are not allowed to be used. You write an eval() expecting simple expressions, and the next thing you know a user is suing you because your code allowed them to enter a string that wiped their hard drive. eval()-like functions are huge gaping security holes and should be avoided.

    The preferred alternative in the .Net world is a DSL (domain specific language). If you google around, you can find some pre-created DSL's for common tasks such as arithmetic.

    0 讨论(0)
提交回复
热议问题