被引用的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#交换方法指针的更多相关文章

  1. ios - runtime运行时应用---交换方法

    runtime运行时用法之一 --- 交换类的方法,此处简单写了把系统的UIView的setBackgroundColor的方法换成了自定义的pb_setBackgroundColor 首先创建UIV ...

  2. Runtime 交换方法

    创建UIImage分类UIImage+Image.h #import<UIKit/UIKit.h> @interfaceUIImage (Image) + (__kindof UIImag ...

  3. objective-c里的方法指针IMP的用法

    SGPopSelectView.h @interface SGPopSelectView : UIView @property (nonatomic, assign) SEL selector; @p ...

  4. iOS项目之交换方法(runtime)

    在项目中,经常会遇到系统自带的方法满足不了自己的需求,往往我们解决这种情况的时候,都是在分类中添加一个方法.然而很多时候,项目已经开发很长时间了,如果一个一个的去替换系统的方法,太浪费宝贵的时间,所以 ...

  5. ios开发runtime学习二:runtime交换方法

    #import "ViewController.h" /* Runtime(交换方法):主要想修改系统的方法实现 需求: 比如说有一个项目,已经开发了2年,忽然项目负责人添加一个功 ...

  6. ios-动态添加方法,交换方法,重定向方法

    新建一个类Person,Person.h 不写代码,Person.m 有如下两个方法: - (void)eat { NSLog(@"xxx eat===="); } [动态添加方法 ...

  7. Objective-C 方法交换实践(二) - 方法指针交换

    一. 基本函数 根据 sel 得到 class 的实例方法 Method class_getInstanceMethod(Class cls, SEL name) 根据 sel 得到 class 的函 ...

  8. 第一个java——swap()交换方法

    其实这学期的java课开了将近四星期了,加上开学前的小小预习.编写不下于二十几个java了. 可这一有关swap()方法的java确实是首次迷惑不解到处寻求解决的程序. 课堂上老师有关类.方法.对象的 ...

  9. Go语言——值方法 & 指针方法

    1 package main import ( "fmt" "sort" ) type SortableStrings []string type Sortab ...

  10. 第28月第5天 uibutton交换方法

    1. //交换系统的方法 @implementation UIControl (MYButton) + (void)load { Method a = class_getInstanceMethod( ...

随机推荐

  1. DataX - [02] 安装部署

    操作系统:Alibaba Cloud Linux release 3 (Soaring Falcon) Java:1.8.0_372 Python:3.6.8 => 2.7.1 一.安装部署 ( ...

  2. 面试题55 - II. 平衡二叉树

    地址:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/ <?php /** 输入一棵二叉树的根节点,判断该树是不是平衡二叉树 ...

  3. iframe高度自适应 完美解决

    前言 一直被iframe的高度自适应的问题困扰着,今天终于找到完美解决方案,加上以下css即可. css iframe { display: block; border: none; height: ...

  4. PVE虚拟平台常用简明操作,三分钟搞定虚拟机更换安装配置

    Proxmox Virtual Environment是一个基于QEMU/KVM和LXC的开源服务器虚拟化管理解决方案,本文简称PVE,与之相类似的虚拟化平台是VMWARE的ESXi虚拟平台,相较于商 ...

  5. TCP协议详细介绍

    TCP报文格式: 字段介绍: 源/目的端口:用来标识主机上的程序 序号(seq):4个byte,指当前tcp报文段中第一个字节的序号(tcp报文中每个字节都有一个编号) 确认号(ack):4个byte ...

  6. 利用AI增强VS Code TypeScript插件:AnyToTS带来编程新体验

    Any to TS: VSCode 扩展插件 概述 "Any to TS" 是一个强大的 VSCode 扩展插件,旨在将任何对象转换为 TypeScript 类型或接口.该工具基于 ...

  7. Hack The Box-Chemistry靶机渗透

    通过信息收集访问5000端口,cif历史cve漏洞反弹shell,获取数据库,利用低权限用户登录,监听端口,开放8080端口,aihttp服务漏洞文件包含,获取root密码hash值,ssh指定登录 ...

  8. 【Python】介绍以及环境搭建

    Python简介 Python介绍 Python是时下最流流.最火爆的编程语言之一,具体原因如下: 简单.易学,适应人群广泛 免费.开源 应用领域广泛 备注:以下知名框架均是Python语言开发. G ...

  9. 【Python】配置pip使用国内镜像源

    配置pip使用国内镜像源 零.问题 使用pip安装插件时总是很慢,咋解决呢? 壹.解决 在桌面上你的文件夹内新建pip目录,一般路径如下:C:\Users\{$你的用户名},比如我的用户名是Minuy ...

  10. 《机器人SLAM导航核心技术与实战》第1季:第11章_自主导航中的数学基础

    <机器人SLAM导航核心技术与实战>第1季:第11章_自主导航中的数学基础 视频讲解 [第1季]11.第11章_自主导航中的数学基础-视频讲解 [第1季]11.1.第11章_自主导航中的数 ...