tags:

views:

67

answers:

2

I have a program, and im not going to go into detail, but i have a void say

void changingcode(string var1, string var2, string var3)
{
}

ok, so i have a bunch of code in a .txt file... and i want to insert it into that void, and i need it to use the variables that are passed to the void. potentially the code could be different every time the void is called. how can i do this?

ill strip it down incase i wasnt clear:

  1. i have a text file full of code
  2. i need to insert the code in the text file into a running program
  3. i need the inserted code to run and act as if it was simply included from the beginning.
  4. the location of the text file with code is included in var1.

note that i cannot have any code breaks or stop the code at any point. -thanks

+5  A: 

You can't 100% pretend it was there from the beginning; you're going to have to work around it.

If the code is in an external text file, you have a number of options:

  • use CSharpCodeProvider to compile the code at runtime; you'd need to add extra fluff to make it a well-defined class (presumably implementing a common interface); then use reflection to create an instance of the type; yeuch - and beware you can't unload, etc.
  • wait until .NET 5.0 and hope that the compiler-as-a-service stays
  • use Mono, where compiler-as-a-service already exists
  • run the external file as (for example) a python script via IronPython (note that this changes the script language)

I'd look at the last option (IronPython) first... seems made for this job.

Marc Gravell
+1 for suggesting the Mono compiler.
Robert Harvey
+1 for many options that you gave.
Wael Dalloul
A: 

You can give scripting languages like Lua or boo a try. I've not implemented these but have seen the similar stuff work in java.

TheVillageIdiot