Can .NET be parsed and evaluated at runtime

I thought it would be great if I could write vb.net or C # code at runtime, and the interpreter automatically parsed it like python does, so I made a small program that would do something like this. It basically looks like this:

InputArgs = Console.ReadLine() ParseInput(InputArgs.Split(" ")) Private Sub ParseInput(Args as List(Of String), Optional TempArgs as List(Of String)) Dim arg as string = Args(0) If arg = ".." then ... elseif arg = ".." then ... end if End Sub 

I know that this is not a good system, but it shows the basics. So my question is: is it possible to make vb.net or C # like python - is interpreted at runtime?

+3
source share
6 answers

It already exists in a variety of forms and forms:

Mono.CSharp

Mono has a Mono.CSharp assembly, which you can reference to do something CSharp.exe (AC # 'interpreter' or interactive shell if you do) can do:

 void ReadEvalPrintLoopWith (ReadLiner readline) { string expr = null; while (!InteractiveBase.QuitRequested){ string input = readline (expr == null); if (input == null) return; if (input == "") continue; expr = expr == null ? input : expr + "\n" + input; expr = Evaluate (expr); } } 

Needless to say, this also works on MS.Net (of course, regarding portable .NET).

The full sources here on github are the same as the rest of Mono.

DLR

Several DLR languages ​​have been implemented, including but not limited to

This will allow you to evaluate python / ruby ​​code on the fly in the .NET framework.

Roslin

Microsoft published Roslyn as CTP (preview). It can basically do the same thing as Mono REPL (first element), and (a lot) more. But this is a preview.

+5
source

It may be, but it will be a lot of work.

One way is to write the + parser itself. To create a parser, you need a grammar definition of the input language, such as C #. You have a complex C # grammar.

Another way is to dynamically compile C # code. Here is an example of how to do this: http://www.voidspace.org.uk/ironpython/dynamically_compiling.shtml and http://blogs.msdn.com/b/saveenr/archive/2009/08/11/a- walkthrough-of-dynamically-compiling-c-code.aspx .

Good luck

+2
source

You do not have much time to give you the correct answer, but here is the eval code from one of my old projects:

  CSharpCodeProvider c = new CSharpCodeProvider(); ICodeCompiler icc = c.CreateCompiler(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("system.dll"); cp.ReferencedAssemblies.Add("system.xml.dll"); cp.ReferencedAssemblies.Add("system.data.dll"); cp.ReferencedAssemblies.Add("system.windows.forms.dll"); cp.ReferencedAssemblies.Add("system.drawing.dll"); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; StringBuilder sb = new StringBuilder(""); sb.Append("using System;\n"); sb.Append("using System.Xml;\n"); sb.Append("using System.Data;\n"); sb.Append("using System.Data.SqlClient;\n"); sb.Append("using System.Windows.Forms;\n"); sb.Append("using System.Drawing;\n"); sb.Append("namespace CSCodeEvaler{ \n"); sb.Append("public class CSCodeEvaler{ " + csCode + "} }"); CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString()); if (cr.Errors.Count > 0) { MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText, "Error evaluating cs code", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } System.Reflection.Assembly a = cr.CompiledAssembly; object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler"); Type t = o.GetType(); MethodInfo mi = t.GetMethod("Transform"); return mi; 

The original version of this code was taken from: http://www.codeproject.com/KB/cs/evalcscode.aspx

It compiles some C # code (the code should be in the csCode variable), then tries to find the Transform () method in it and returns its MethodInfo, so we can execute it.

But remember that every time you call this code, a new assembly is loaded, so do not use it too often.

Also, as Prescott said, try Roslyn

Hope this helps. Sorry for not providing a code example more specific to your question.

PS. If you want some kind of interactive window, there are third-party controls for this (use Google to find, because I do not remember the exact names of the products)

+1
source

Are you reinventing LinqPad ?

LinqPad is a free ergonomic C # / VB / F # scratchpad that instantly executes any expression, statement block or program with rich output formatting. Excellent.

If you really want to develop your own, look at some of the similar existing questions one two three four ...

+1
source

So my question is: is it possible to make vb.net or C # like python - is interpreted at runtime?

Of course. Start writing a translator, and then you can interpret it. Start by parsing the syntax. Then go from there.

0
source

Not that I know. But as a hack, you can create textarea that has a TextChanged event handler that calls the compiler program (not sure where it is).

0
source

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


All Articles