Compiling and Calling C# on-the-fly

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

In my C# client-side application, I want to give the user
the ability to run algorithms that they write. Given the
rich features of VB and C# (and Java), I would like to
enable the user to write their algorithms in these
languages. My program should be able to interface to the
algorithm and run it on-the-fly. It should even let the
user debug their algorithms with .NET Visual Studio.

Has anyone done this? Is their a good guide, book, or
tutorial that would explain what would be necessary?

Thanks!
 
I've done something like that in the following article:
http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/datagrid.mspx
The first problem that needs to be solved is: how can we evaluate
expressions, which are stored in a string value at runtime? The first
thought could be: we have to build an interpreter that splits the string
into pieces that we can execute one by one. But this approach would be quite
difficult to build, especially if you want to support a lot of functions.
For example imagine to support ToString, Length, ToUpper, ... But luckily
there is the CodeDom in the .NET Framework that is just what we need!
CodeDom stands for Code Document Object Model, and like what an XMLDocument
is for XML, the CodeDom is for code. With the CodeDom you can describe code
in an object oriented way, like an XMLDocument is a way to describe XML in
an object oriented way. Each code structure has its own class in the
CodeDom, so it's possible to build language-independent code at run-time.
That's right, CodeDom is language-independent, and if you think about it,
it's quite normal: each language has its If-Then, For-Each, ... structures.
Once you have a built code with the CodeDom you can do two things with it:
code generation or compilation. Because the CodeDom is language independent
it's quite easy to generate code for any language. But code compilation is
what we need, we want to be able to execute an expression and get the return
value.
 
Back
Top