Skip to content

A simple runtime library that you can use to compile and execute C# code dynamically.

Notifications You must be signed in to change notification settings

IAmTheTable/Runtime-Code-Execution

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Runtime-Code-Execution

A project I created to execute C# code dynamically on runtime!

  • Preview image

How to use

  • 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...
      }
    }
  }
}

About

A simple runtime library that you can use to compile and execute C# code dynamically.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages