标题或多或少说明了一切。基于这篇文章,我想出了这个:
public static unsafe void Replace(this MethodBase destination, MethodBase source)
{
IntPtr srcHandle = source.MethodHandle.GetFunctionPointer();
IntPtr dstHandle = destination.MethodHandle.GetFunctionPointer();
int* dstPtr = (int*)dstHandle.ToPointer();
*dstPtr = srcHandle.ToInt32();
}
这实际上有效......偶尔-.-
例如,这有效。
public static class Program
{
public static void Main(string[] args)
{
MethodInfo methodA = typeof(Program).GetMethod("A", BindingFlags.Public | BindingFlags.Static);
MethodInfo methodB = typeof(Program).GetMethod("B", BindingFlags.Public | BindingFlags.Static);
methodA.Replace(methodB);
A();
B();
}
public static void A()
{
Console.WriteLine("Hai World");
}
public static void B()
{
Console.WriteLine("Bai World");
}
}
但是,这不是(SEHException)。我所做的只是改变定义函数的顺序。
public static class Program
{
public static void Main(string[] args)
{
MethodInfo methodA = typeof(Program).GetMethod("A", BindingFlags.Public | BindingFlags.Static);
MethodInfo methodB = typeof(Program).GetMethod("B", BindingFlags.Public | BindingFlags.Static);
methodA.Replace(methodB);
A();
B();
}
public static void B()
{
Console.WriteLine("Bai World");
}
public static void A()
{
Console.WriteLine("Hai World");
}
}
至于文章中的代码......我根本无法让它工作。
有什么想法/替代方案吗?