using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Diagnostics;
// 工具定义
// 注意: 需要 "unsafe编译" ([项目属性 - 生成] 勾选"允许不安全代码")
// 仅仅实现了 x64支持
namespace MethodReplace
{
public static class MethodReplaceUtil
{
public static bool ReplaceMethod(MethodInfo method, MethodInfo new_method)
{
if (IntPtr.Size != 8 || new_method.IsVirtual)
{
// 仅仅支持 x64
// 替换用的函数必须是普通函数
return false;
}
RuntimeHelpers.PrepareMethod(method.MethodHandle);
RuntimeHelpers.PrepareMethod(new_method.MethodHandle);
try
{
// 虚函数
if (method.IsVirtual)
{
return ReplaceVirtualMethod(method, new_method);
}
// 普通函数
#if DEBUG
const bool buildModelDebug = true;
#else
C#运行时替换类的成员函数
最新推荐文章于 2021-09-29 18:06:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)