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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
I guess that seems to be the only way... Thanks, I will do it like that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.