Dreamer78692
Dreamer78692

Reputation: 215

Dynamically Compile Methods in a Class Library

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.

Upvotes: 1

Views: 261

Answers (1)

cynic
cynic

Reputation: 5405

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.

Upvotes: 1

Related Questions