-1

I have WinForm called Form1 and there are Button1, MemoEdit1 and 2 TextBoxes named TextBox1 and TextBox2. At runtime user should be able write C# code in MemoEdit1 in order to manipulate the TextBox controls. F.e: at runtime user typed into MemoEdit1 simple code like: TextBox2.Text = "Hello" + TextBox1.Text;

So, when I click on Button1, I need to compile and execute the code.

Question may sound so simple as I am a newbie in compiling/executing code during runtime in C#.

Could you pls, help?

Thanks.

Tim
  • 459
  • 1
  • 4
  • 20
  • 1
    What do you mean "dynamic code" and why did you tag this question `codedom`? – User 12345678 Jan 30 '15 at 15:03
  • 1
    Yes the line you wrote is possible, why not try it instead of asking? – Ehsan Sajjad Jan 30 '15 at 15:10
  • I think there is something missing in the question; but for now what you asked is possible. – mcy Jan 30 '15 at 15:11
  • I know I will have to use CodeDom for compiling user supplied (that is why I called it dynamic) code, but I don't know how to reference Form1 Controls (in this example TextBox1 and TextBox2). Thanks. – Tim Jan 30 '15 at 15:24
  • There is still nothing in your question that actually deals with compiling user provided code. It only appears that your question deals with how to concatenate a string constant with a string variable - which if that is your question I recommend that you take some time to find some beginners C# books and have a read. Otherwise you need to modify your question to better specify what you're asking - help us help you. – Anthony Jan 30 '15 at 15:38
  • http://en.m.wikipedia.org/wiki/.NET_Compiler_Platform – AK_ Jan 30 '15 at 22:29

1 Answers1

0

Take a look on this snippet

public class Evaluator
{
    public void Eval(string Code)
    {
        Microsoft.CSharp.CSharpCodeProvider Provider = new Microsoft.CSharp.CSharpCodeProvider(); // Create an provider

        System.CodeDom.Compiler.ICodeCompiler Compiler = Provider.CreateCompiler(); // Create An Compiler

        System.CodeDom.Compiler.CompilerParameters Parameters = new System.CodeDom.Compiler.CompilerParameters(); // Create a parameters of the compiler

        Parameters.GenerateInMemory = true; // It should generate the compiled assembly in the memory

        System.CodeDom.Compiler.CompilerResults Results = Compiler.CompileAssemblyFromSource(Parameters, Code); //Compile it

        ///Now you just need to use reflection to call its methods
        object SomeClass = Results.CompiledAssembly.CreateInstance("ClassName"); //Name of the class you want to create an instance
        var Method = SomeClass.GetType().GetMethod("MethodName"); //Name of the Method you want to call
        Method.Invoke(SomeClass, null); // change null for the argument it needs
    }
}

if you want to just write code you will have to add the an class and a Method to wrap the user code and then you call it through the Invoke, you will probably have to reference your own assembly into this assembly

Patrick
  • 736
  • 9
  • 27