使用带有Mono.Cecil的callvirt时无效的IL代码 - C#

时间:2015-06-04 21:25:42

标签: c# il mono.cecil

我试图执行此代码:

Assembly.LoadFrom(Path.Combine(Application.dataPath, "Mods/VapidModLoader.dll")).GetType("Vapid.ModLoader.ModLoaderActivator").GetMethod("Activate").Invoke(null, null);

在我尝试这样做时,我使用Mono.Cecil写了这个。

ILProcessor processor = gameAwakeMethod.Body.GetILProcessor();
processor.Body.Instructions.Clear(); // Clear old instructions

processor.Body.Instructions.Insert(0, processor.Create(OpCodes.Call, ImportMethod(game, typeof(Application), "get_dataPath")));
processor.Body.Instructions.Insert(1, processor.Create(OpCodes.Ldstr, "Mods/VapidModLoader.dll"));
processor.Body.Instructions.Insert(2, processor.Create(OpCodes.Call, ImportMethod(game, typeof(Path), "Combine")));
processor.Body.Instructions.Insert(3, processor.Create(OpCodes.Call, ImportMethod(game, typeof(Assembly), "LoadFrom")));
processor.Body.Instructions.Insert(4, processor.Create(OpCodes.Ldstr, "Vapid.ModLoader.ModLoaderActivator"));
processor.Body.Instructions.Insert(5, processor.Create(OpCodes.Callvirt, ImportMethod(game, typeof(Assembly), "GetType")));
processor.Body.Instructions.Insert(6, processor.Create(OpCodes.Ldstr, "Activate"));
processor.Body.Instructions.Insert(7, processor.Create(OpCodes.Callvirt, ImportMethod(game, typeof(Type), "GetMethod")));
processor.Body.Instructions.Insert(8, processor.Create(OpCodes.Ldnull));
processor.Body.Instructions.Insert(9, processor.Create(OpCodes.Ldnull));
processor.Body.Instructions.Insert(10,processor.Create(OpCodes.Callvirt, ImportMethod(game, typeof(MethodBase), "Invoke")));
processor.Body.Instructions.Insert(11,processor.Create(OpCodes.Pop));

我的ImportMethod方法如下所示

private static MethodReference ImportMethod(AssemblyDefinition assembly, Type type, string name)
{
    var importedType = assembly.MainModule.Import(type);
    return assembly.MainModule.Import(importedType.Resolve().Methods.First(f => f.Name == name));
}

这是我得到的错误:

  

InvalidProgramException:PlanetRotateMouse中的IL代码无效:启动   ():IL_0023:callvirt 0x0a000235

这是编译时IL代码的样子 enter image description here

很抱歉,如果我没有足够的详细信息,但我包含了所有相关内容,而且我对这些主题的了解并不多。任何有关出错的帮助或解释将不胜感激。谢谢!

0 个答案:

没有答案