.netwinformsc#-4.0reflection

running external C# file in a program


Does the .NET framework have any classes which allow you to run (compile, interpret or whatever) an external script file containing C# code?

For instance, if I have a file Hello.cs containing this:

class Hello
//This program displays Hello World
{
    static public void Main()
    {
        System.Console.WriteLine("Hello World");
    }
}

how can i load the code above, from within a winform, app and execute it?

I'm interested in the load/execute logic; the program could be anything, from a console app to another winform app.

Does Reflection allow this?


Solution

  • You can perform "on-the-fly" compilation.

    Check out the following article for more: C#: Writing extendable applications using on-the-fly compilation.