in c#: expression evaluation function like flash script

▼魔方 西西 提交于 2019-11-28 08:21:27

问题


Duplicate of: How can I evaluate a C# expression dynamically?

See also: C# eval equivalent?

How to evaluate expression. Maybe like:

int a=1;
int b=3;
int c=Eval("a+b");

or

int c=int.parse("1+3*(2+3)");

This seems stupid to me. is it possible in c#?


回答1:


You can take code, and using the CSharpCodeProvider write an Eval function that actually compiles your code into an in-memory assembly and then executes that code.

See this CodeProject article for sample source.




回答2:


Not directly. C# contains no runtime compiler. There is an open source project attached to Mono that will do this.




回答3:


I enumerate several different approaches that I am aware of in this answer to "How can I evaluate a C# expression dynamically" which include various .NET framework solutions such as CodeDomProvider, DataBinder.Eval, DataTable.Compute, Document.InvokeScript, and more.



来源:https://stackoverflow.com/questions/534894/in-c-expression-evaluation-function-like-flash-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!