Question

I remember with InterSystems Cache code, you can use indirection to take a string and turn that into real executable code by preceding the string variable with "@". Can this be done in C#.NET or VB.NET code? So I'd like to have a method that would take an arguments array of strings (with one or multiple lines of code), and run that code, assuming it doesn't throw an exception of course. Where am I going with this? I'm trying to write a compiler within .NET code.

SET x="set a=3" XECUTE x   ; sets the public variable a to 3

OR

SET x="tag1" d @x  ; do/call the public subroutine tag1

OR 

Set Y = "B",@Y = 6  ; sets public variable B = 6
Was it helpful?

Solution

I assume that you want to compile during runtime.
System.CodeDom and System.CodeDom.Complier namespaces contain interfaces that are relevant to runtime compilation.
For your own language you need to implement your derived class from a derived class of CodeDomProvider.

OTHER TIPS

For .NET you can either programmatically build up code using System.CodeDom which is basically a wrapper over the Intermediate Language, or you can use System.CodeDom.Compiler to get an object that compiles a string (or file) into an executable or DLL using a C# or VB.NET compiler.

Compiling the string is more like the Intersystems Cache way of doing it, but it's still more work, because you must provide all the information the compiler needs. If you look at the CompilerParameters class you will see the added complexity. The compiled code will be in it's own assembly. An assembly can't be unloaded unless it's in it's own App Domain, and when dynamically compiling it is difficult enough that most people don't bother if they can avoid it.

Various approaches to your problem are proposed on this very site.

Some source code for one solution to what you've described can be found here if the link stays alive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top