被引用的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. autMan奥特曼机器人-跳过注册页面直接进入登陆页面

    1.将下面4行内容存成txt文件[注意将"账号"和"密码"等字眼替换为自己的真正账号和密码],文件名改成sets.conf,放到autman主文件夹,见下图最下 ...

  2. MySQL - [10] 时间处理函数

    题记部分 (1)获取当前日期时间:select current_date; (2)获取当前时间戳:select current_timestamp; (3)返回日期中的年/季度/月/日/时/分/秒 s ...

  3. P5355 [Ynoi Easy Round 2017] 由乃的玉米田

    莫队 + bitset + 根号分支 乘法似乎是简单的,我们可以直接莫队扫描然后枚举较小数 时间 \((n + m) \sqrt n\). 加法是一个经典 idea, 莫队套 bitset,然后利用 ...

  4. 晶振测试仪GDS-80系列参数

    晶振测试仪GDS-80系列 一.产品简介 晶振测试仪GDS-80系列是高性价比的晶振测试系统,采用网络分析技术,实现智能化测量,符合IEC-444标准.测量频率范围10KHz-200KHz,1MHz- ...

  5. New Bing 全面开放?我看未必

    前段时间大家应该都被ChatGPT刷屏了,其实就回答来说New Bing 才是最厉害的,因为它底层使用了ChatGPT 并且可以支持联网查询数据,回答中还能支持看到出处,方便确认其真实性. New B ...

  6. datagrid源码

    /** * jQuery EasyUI 1.2.3 * * Licensed under the GPL terms * To use it on other terms please contact ...

  7. laravel-admin php artisan admin:install报错问题解决办法

    问题描述:根据laravel-admin官方文档安装步骤,执行:php artisan admin:install 安装时报错. 其实是数据库创建特殊字符过长问题,laravel 5.4 改变了默认的 ...

  8. 如何通过 MCP 将你的 Supabase 数据库连接到 Cursor

    Cursor + MCP + Supabase. 图片来自作者 在过去几周里,MCP(Model Context Protocol,模型上下文协议)在许多 AI 相关的在线社区和论坛里大火.开发者和技 ...

  9. Windows 提权-服务_弱注册表权限

    本文通过 Google 翻译 Weak Registry Key Permissions – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭 ...

  10. 【Pandas】concat方法使用注意点

    使用pandas库的concat做数据拼接需要注意,应该先对原始数据设置列名,如果没有设置列名,在拼接时只会保留第一个文件的第一行(以列名形式),由于剩下的文件在读取时会自动将第一行作为列名,这样就会 ...