Run C # command stored in String variable?

Possible duplicate:
equivalent c # equivalent?

Is it possible to execute a C # command that is stored in a string variable

such a thing as a dynamic query in SqlServer

+4
source share
2 answers

Nothing is built into the current version of .NET, which allows this. However, the Roslyn project aims to incorporate scenarios such as this. They have a CTP out .

A simple example: (from the blog of Kirill Osenkov ):

[TestMethod] public void SimpleEvaluationUsingScriptEngine() { ScriptEngine engine = new ScriptEngine(); int result = engine.Execute<int>("1 + 2"); Assert.AreEqual(3, result); } 
+1
source

You can use CSharpCodeProvider to compile the source code from a file or from strings to an assembly that you can load. Then you can call the generated classes using reflection.

0
source

Source: https://habr.com/ru/post/1392082/


All Articles