如何执行字符串中的代码?

Vac*_*ano 12 c#

说我有这样的事情:

string singleStatement = "System.DateTime.Now";
Run Code Online (Sandbox Code Playgroud)

有没有办法singleStatement在运行时采取和解析并运行它?

以便:

DateTime currentTime = singleStatement.SomeCoolMethodToRunTheText();
Run Code Online (Sandbox Code Playgroud)

会指定DateTime.Nowto 的值currentTime.

Ali*_*tad 1

这只有通过使用编译器服务才能实现,编译器服务Reflection.Emit()将进行编译、汇编并加载到内存中。

看看这里。

http://www.c-sharpcorner.com/UploadFile/puranindia/419/