Zooey Zooey - 10 days ago 6
C# Question

Adding static constructor with Mono.Cecil causes TypeInitializationException

I am trying to add a static constructor using Mono Cecil to a program like the following:

namespace SimpleTarget
{
class C
{
public void M()
{
Console.WriteLine("Hello, World!");
}
}
}


The following code adds the static constructor:

namespace AddStaticConstructor
{
class Program
{
static void Main(string[] args)
{
var assemblyPath = args[0];
var module = ModuleDefinition.ReadModule(assemblyPath);

var corlib = ModuleDefinition.ReadModule(typeof(object).Module.FullyQualifiedName);
var method = corlib.Types.First(t => t.Name.Equals("Console")).Methods.First(m => m.Name.Contains("WriteLine"));

var methodToCall = module.Import(method);

foreach (var type in module.Types)
{
if (!type.Name.Contains("C")) continue;

var staticConstructorAttributes =
Mono.Cecil.MethodAttributes.Private |
Mono.Cecil.MethodAttributes.HideBySig |
Mono.Cecil.MethodAttributes.Static |
Mono.Cecil.MethodAttributes.SpecialName |
Mono.Cecil.MethodAttributes.RTSpecialName;

MethodDefinition staticConstructor = new MethodDefinition(".cctor", staticConstructorAttributes, module.TypeSystem.Void);
type.Methods.Add(staticConstructor);

type.IsBeforeFieldInit = false;

var il = staticConstructor.Body.GetILProcessor();
il.Append(Instruction.Create(OpCodes.Ret));

Instruction ldMethodName = il.Create(OpCodes.Ldstr, type.FullName);
Instruction callOurMethod = il.Create(OpCodes.Call, methodToCall);

Instruction firstInstruction = staticConstructor.Body.Instructions[0];
// Inserts the callOurMethod instruction before the first instruction


il.InsertBefore(firstInstruction, ldMethodName);
il.InsertAfter(ldMethodName, callOurMethod);
}

module.Write(assemblyPath);
}
}
}


Looking at the decompiled binary in dotPeek, it appears as if everything is setup correctly. When trying to use the modified
C
type, I get a TypeInitializationException with the inner exception "System.InvalidProgramException: JIT Compiler encountered an internal limitation"

Is there anything else I need to set correctly before using a static constructor?

Thanks!

Answer

The problem is that you are getting the wrong overload of System.WriteLine here:

var corlib = ModuleDefinition.ReadModule(typeof(object).Module.FullyQualifiedName);
var method = corlib.Types.First(t => t.Name.Equals("Console")).Methods.First(m => m.Name.Contains("WriteLine"));
var methodToCall = module.Import(method);

use this simple code the get the overload you want to use:

var wlMethod = typeof (Console).GetMethod(nameof(Console.WriteLine), new[] {typeof (string)});
var methodToCall = module.ImportReference(wlMethod);