lundi 14 mars 2022

How do I run C# code that's in a textfile from another C# app

So I have this textfile that looks like this, and I'd like to create a console application that reads the content of it and invokes the "Main" method.

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

And I tried doing this using CSharpCodeProvider but the issue there is that I would have to add it as a dependency to the project since I'm using .NET 6 and I would prefer to do it without having to add any extra dependencies.

The only thing I have so far is that I read the content of the textfile, but I have no idea how to invoke the "Main" method

static void Main(string[] args)
{
    var code = File.ReadAllLines("csharpcode.txt");

}

How do I properly invoke the Main method from my application by reading code from a textfile?





Aucun commentaire:

Enregistrer un commentaire