Search code examples
c#.netildynamic-method

Emit IL OpCodes from C# source code


How to parse C# source code for generating IL opcodes, that could be used in DynamicMethod?

I want to execute code dynamically without generating unnecessary assemblies. Something like this:

var body = "return \"sample\";";
var dm = new DynamicMethod("method_" + Guid.NewGuid().ToString("N"), typeof(string), null);
var parser = new SomeKindOfCSharpParser();
parser.Emit(body, m.GetILGenerator());

Solution

  • Roslyn should be something close to this...