我正在尝试更改第三方库中的一个部件,它有一个错误,但是,没有修复程序并且源代码也不可用。
一开始,我试图通过反汇编直接修改IL Opcodes,但它有某种保护,所以重新编译它后,即使我从中删除了它也无法使用Strong Name verification它。
我试图IL instructions在运行时改变,但是
InjectionHelper.Initialize();
InjectionHelper.WaitForIntializationCompletion();
Type type = obj.GetType();
var methods = type.GetMethods();
// MethodInfo targetMethod = type.GetMethod("MethodName", BindingFlags.NonPublic | BindingFlags.Instance);
var methodInfo = type
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Single(
m =>
m.ReturnType == typeof(string) &&
m.GetParameters().Length == 1);
RuntimeHelpers.PrepareMethod(methodInfo.MethodHandle);
byte[] ilOpCodes = methodInfo.GetMethodBody()?.GetILAsByteArray();
for (int i = 0; i < ilOpCodes.Length; i++)
{
if (ilOpCodes[i] == OpCodes.Ldc_I4_1.Value)
{
ilOpCodes[i] = (byte) OpCodes.Ldc_I4_0.Value;
break;
}
}
InjectionHelper.UpdateILCodes(methodInfo,ilOpCodes);
但不幸的是有一个例外。
Unhandled Exception: System.Exception: UpdateILCodes() failed, please check the initialization is failed or uncompleted.
我犯了什么错误吗?我使用这篇文章作为参考https://www.codeproject.com/Articles/463508/NET-CLR-Injection-Modify-IL-Code-during-Run-time 那么让它可用的最佳方法是什么?