A project I created to execute C# code dynamically on runtime!
- Construct the class
// dotnet < 5
CodeModule module = new CodeModule();
// dotnet 5 >=
CodeModule module = new();
- Define your code
string _code = @"
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(args == null ?? string.Join("" "", args));
}
}
";
- Load and execute your code
var codeResult = module.Load(_code, out EmitResult result); // returns true if successful
if(!codeResult)
Console.WriteLine("Failed to load code"); // false, commonly for syntax errors and what not...
else
{
Console.WriteLine("Successfully compiled code");
module.Execute(); // you can pass string args here as if it were directly passed into the Main function...
}
- Full example
using System;
using RuntimeCodeExec;
namespace YOUR_PROJECT
{
public class Program
{
public static void Main(string[] args)
{
CodeModule module = new CodeModule();
string _code = @"
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(args == null ?? string.Join("" "", args));
}
}
";
var codeResult = module.Load(_code, out EmitResult result); // returns true if successful
if(!codeResult)
Console.WriteLine("Failed to load code"); // false, commonly for syntax errors and what not...
else
{
Console.WriteLine("Successfully compiled code");
module.Execute(); // you can pass string args here as if it were directly passed into the Main function...
}
}
}
}