1

Say you have This, which is compiled into a dll:

namespace HelloWorld
{
   public class Hello
   {
        public void WriteHello()
        {
             Console.Writeline("Hello World");
        }

        **public void WriteHello2()
        {
             Console.Writeline("Hello World2");
        }**
   }
}

I want the method WriteHello2 to actually be in a text file and compiled from there, in this program.

REASON

I want to create an interface where the user creates a method. Then I will write this method into a text file, and so whenever the program is run it will read in the text file, and execute that piece of code.

Example

Say my dll only consists of 1 method i.e. WriteHello. The user runs the program, and then creates a new method say WriteHello2( he cannot create any method it is limited to my application). Now the dll should contain 2 methods.

I am not sure if any of this is possible.

Dreamer78692
  • 215
  • 5
  • 20
  • It's absolutely possible and its discussed in some other questions. Mostly in c# so you will have to translate to VB :( [Add scripting support to c#/.net app](http://stackoverflow.com/questions/1067784/c-net-scripting-library), and particularly the accepted answer to [embedding a scripting language into a c# desktop app](http://stackoverflow.com/questions/137933/what-is-the-best-scripting-language-to-embed-in-a-c-sharp-desktop-application) – MarkJ Nov 10 '12 at 12:15

1 Answers1

1

To compile the code from a string, you can use the CompileAssemblyFromSource method. Things become more complicated if you want the changes to be persisted, that is, to have the program self-modify its binaries. I'm not sure if that's what you want, but it's doable with some clever temporary file juggling and process coordination.

cynic
  • 5,305
  • 1
  • 24
  • 40
  • I have tried that but I had put the whole program into the text file, I am not sure of using CompileAssemblyFromSource, to put bits and pieces into the text file. – Dreamer78692 Nov 09 '12 at 07:03
  • You do need to have the source for the entire dll available (you can't add code to the dll, you can only recreate it whole). In your particular source example, you could make the `Hello` class partial, and have a separate "template" file of sorts that contains the namespace and class declaration, in which you paste the user-supplied string with just the method. – cynic Nov 09 '12 at 07:06
  • I guess that seems to be the only way... Thanks, I will do it like that. – Dreamer78692 Nov 09 '12 at 07:12