Ich versuche, einen statischen Konstruktor Mono Cecil zu einem Programm wie folgt hinzuzufügen:statischen Konstruktor mit Mono.Cecil Hinzufügen verursacht TypeInitializationException
namespace SimpleTarget
{
class C
{
public void M()
{
Console.WriteLine("Hello, World!");
}
}
}
Der folgende Code fügt den statischen Konstruktor:
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);
}
}
}
Betrachtet man die dekompilierte Binärdatei in dotPeek, sieht es so aus, als wäre alles korrekt eingerichtet. Wenn ich versuche, den modifizierten C
-Typ zu verwenden, erhalte ich eine TypeInitializationException mit der inneren Ausnahme "System.InvalidProgramException: JIT-Compiler hat eine interne Einschränkung gefunden"
Gibt es noch etwas, das ich richtig einstellen muss, bevor ich einen statischen Konstruktor benutze?
Danke!
Bitte senden Sie uns eine [MCVE] . –