Can you perform code indirection in .NET?

Go To


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


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


Set Y = "B",@Y = 6  ; sets public variable B = 6
2012-04-04 05:35
by MacGyver
Take a look at Roslyn - cordialgerm 2012-04-04 05:40
I like how the obscure Caché is the one you picked for example of indirection. Did you happen to work for a healthcare company - NH. 2017-03-23 17:17
Haha! Yah, I worked for Epic for 5.5 years. You too - MacGyver 2017-03-23 20:03
oops, a year an a half later I finally see your reply. Yes, I worked there for 3.5 years - NH. 2018-12-13 16:58
Nice. What do you do now? I work for an Epic customer now. So it's payback time! Lo - MacGyver 2018-12-14 08:56


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.

2012-04-04 05:52
by weismat
Cool! Is this equivalent to calling csc * from the command line? Or running msbuild *.sln .. from within .NET code - MacGyver 2012-04-04 05:55
This is scripting - thus this is compilation during runtime.
csc means native compilation during compile time - weismat 2012-04-04 06:07
do you recommend any references/links for some reputable examples of using this namespace? I'll play around with it in the mean time - MacGyver 2012-04-04 21:05
is this possible in Java - MacGyver 2012-04-26 07:26


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.

2012-04-05 17:21
by psr
is this possible in Java? :- - MacGyver 2012-04-26 07:26