首页 > 编程语言 >C#交换方法指针

C#交换方法指针

时间:2022-09-29 19:12:02浏览次数:54  
标签:C# 交换 System TestAM MethodHandle var using public 指针

被引用的dll是testDllFr.dll,其代码为:


namespace testDLLFr
{
    public class TestA
    {
        public static void TestAM()
        {
            Console.WriteLine("TestAM");
        }
    }
    public class Test
    {
        public static void TestDLl()
        {
            TestA.TestAM();
        }
    }
}

引用者为FrameWorkTest.dll,其代码为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using testDLLFr;

namespace FrameWorkTest
{
    public class TestB
    {
        public static void TestBM()
        {
            Console.WriteLine("testbm");
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Assembly assm = Assembly.GetAssembly(typeof(testDLLFr.TestA));
            Type[] ts = assm.GetTypes();
            var ta = ts.Where(n => n.Name == "TestA").FirstOrDefault();
            MethodInfo tm = ta.GetMethod("TestAM"), tmb = typeof(TestB).GetMethod("TestBM");
            ExchangeMethod(tm, tmb);
            testDLLFr.Test.TestDLl();
            Console.ReadKey();
        }
        private static bool ExchangeMethod(MethodInfo targetMethod, MethodInfo injectMethod)
        {
            if (targetMethod == null || injectMethod == null)
            {
                return false;
            }
            RuntimeHelpers.PrepareMethod(targetMethod.MethodHandle);
            RuntimeHelpers.PrepareMethod(injectMethod.MethodHandle);
            unsafe
            {
                if (IntPtr.Size == 4)
                {
                    int* tar = (int*)targetMethod.MethodHandle.Value.ToPointer() + 2;
                    int* inj = (int*)injectMethod.MethodHandle.Value.ToPointer() + 2;
                    var relay = *tar;
                    *tar = *inj;
                    *inj = relay;
                }
                else
                {
                    long* tar = (long*)targetMethod.MethodHandle.Value.ToPointer() + 1;
                    var a = targetMethod.MethodHandle;
                    var b = a.Value;
                    var c = b.ToPointer();
                    var d = (long*)c;
                    var e = d + 1;
                    long* inj = (long*)injectMethod.MethodHandle.Value.ToPointer() + 1;
                    var relay = *tar;
                    *tar = *inj;
                    *inj = relay;
                }
            }
            return true;
        }
    }
}

FrameworkTest中调用了testDLLFr中Test的TestDLL方法,而该方法调用了TestA的TestAM方法,现在我们想用我们自己的TestB的TestBM方法来替换TestAM的指针。
运行发现:

testbm

即,交换成功。

标签:C#,交换,System,TestAM,MethodHandle,var,using,public,指针
From: https://www.cnblogs.com/johnyang/p/16742674.html

相关文章

  • 序列化成Json时,多个对象互相引用导致死循环 Text.Json.JsonException: A possible obj
    错误:当两个类中的属性互相引用时,导致对象实例序列化成Json时死循环,错误如下:System.Text.Json.JsonException:Apossibleobjectcyclewasdetected.Thiscaneither......
  • 【C#】一幅图解释Class和Delegate 的本质 - 一体双生
    本文纯粹是个人感悟:一个事物=数据+行为,为了定义这两个。.net派生出了Class和Delegate。Class:用于封装数据,Delegate:用于封装行为。于是就有了.net系统的属性和索引、......
  • scrapy
    scrapy项目搭建目录scrapy项目搭建中文文档一、scrapy运行原理二、工程搭建流程三、数据抓取步骤四、目录文件说明五、基础配置修改中文文档https://docs.pythontab.co......
  • SpringCloud学习 系列四、微服务中心 Eureka介绍及创建一个Eureka中心服务
    不得不先介绍一个概念1、CAP 定理(1)概念CAP定理指的是在一个分布式系统中,Consistency(一致性)、Availability(可用性)、Partitiontolerance(分区容错性),三者不可兼得。......
  • vue3 pc端页面全屏
    先看效果图: 附上代码: <template><el-button@click="hand">{{fullscreen?'取消全屏':'点击全屏'}}</el-button></template><script>import{ref}......
  • Belt Conveyor
    ProblemStatementWehaveagridwith$H$horizontalrowsand$W$verticalcolumns.$(i,j)$denotesthesquareatthe$i$-throwfromthetopand$j$-thcolu......
  • 跨域 jsonp/CROS
    jsonp是jsonwithpadding的缩写,它不属于Ajax请求,但它可以模拟Ajax请求。封装jsonp 文件functionjsonp(options){//动态创建script标签varscri......
  • Django InspectDB 缺少mysql/mariadb的注释问题。
    我的是3.13.1。修改2处文件。1.django\core\management\commands\inspectdb.py2.django\db\backends\mysql\introspection.py1.inspectdb.py@@-213,6+213,8@@clas......
  • SARscape5.6.2形变结果分类工具介绍
    SARscape5.6.2版本干涉叠加模块新增时序分类工具,可对时序InSAR进行高级后处理和分析。该工具通过现象特征和参数分析对位移时间序列进行自动分类。可使用外部气象数据(雨、......
  • Iroha and Haiku (New ABC Edition)
    ProblemStatementThereisasequence$A=(A_0,\ldots,A_{N-1})$oflength$N$.Determineifthereexistsatupleofintegers$(x,y,z,w)$thatsatisfiesalloft......