Can you perform code indirection in .NET?

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

Answers


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.


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.


Need Your Help

tomcat startup script error

linux tomcat centos

i needed to change the tomcat process to be executed by non root user. created user tomcat and put that in tomcat_group group. changed permissions. and then changed the startup script in init.d.

How to create a naive location service for building specific accuracy

android geolocation wifi router wlan

I want to build a prototype app that allows users to rope off a building such that when others open the app in that roped off area they can be located to that building without fail. Current geoloca...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.