C#交换方法指针
被引用的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#交换方法指针的更多相关文章
- ios - runtime运行时应用---交换方法
runtime运行时用法之一 --- 交换类的方法,此处简单写了把系统的UIView的setBackgroundColor的方法换成了自定义的pb_setBackgroundColor 首先创建UIV ...
- Runtime 交换方法
创建UIImage分类UIImage+Image.h #import<UIKit/UIKit.h> @interfaceUIImage (Image) + (__kindof UIImag ...
- objective-c里的方法指针IMP的用法
SGPopSelectView.h @interface SGPopSelectView : UIView @property (nonatomic, assign) SEL selector; @p ...
- iOS项目之交换方法(runtime)
在项目中,经常会遇到系统自带的方法满足不了自己的需求,往往我们解决这种情况的时候,都是在分类中添加一个方法.然而很多时候,项目已经开发很长时间了,如果一个一个的去替换系统的方法,太浪费宝贵的时间,所以 ...
- ios开发runtime学习二:runtime交换方法
#import "ViewController.h" /* Runtime(交换方法):主要想修改系统的方法实现 需求: 比如说有一个项目,已经开发了2年,忽然项目负责人添加一个功 ...
- ios-动态添加方法,交换方法,重定向方法
新建一个类Person,Person.h 不写代码,Person.m 有如下两个方法: - (void)eat { NSLog(@"xxx eat===="); } [动态添加方法 ...
- Objective-C 方法交换实践(二) - 方法指针交换
一. 基本函数 根据 sel 得到 class 的实例方法 Method class_getInstanceMethod(Class cls, SEL name) 根据 sel 得到 class 的函 ...
- 第一个java——swap()交换方法
其实这学期的java课开了将近四星期了,加上开学前的小小预习.编写不下于二十几个java了. 可这一有关swap()方法的java确实是首次迷惑不解到处寻求解决的程序. 课堂上老师有关类.方法.对象的 ...
- Go语言——值方法 & 指针方法
1 package main import ( "fmt" "sort" ) type SortableStrings []string type Sortab ...
- 第28月第5天 uibutton交换方法
1. //交换系统的方法 @implementation UIControl (MYButton) + (void)load { Method a = class_getInstanceMethod( ...
随机推荐
- OpenLayers 绘制带箭头的LineString
<!--******************************************************************** * Copyright 2000 - 2022 ...
- Kettle - 使用案例
原文链接:https://blog.csdn.net/gdkyxy2013/article/details/117106691 案例一:把seaking的数据按id同步到seaking2,seakin ...
- 有关算法与数据结构的考题解答参考汇总 [C++] [链表] · 第三篇
早先年考研的主考科目正是[算法与数据结构],复习得还算可以.也在当时[百度知道]上回答了许多相关问题,现把他们一起汇总整理一下,供读者参考. [1] 原题目地址:https://zhidao.baid ...
- .NET 10 首个预览版发布,跨平台开发与性能全面提升
前言 2024年2月25日,微软正式推出 .NET 10 预览版 1,标志着这一跨平台开发框架迈入新里程碑. 本次更新聚焦 JIT 编译器优化.运行时性能提升和跨平台开发体验增强,同时引入多项开发者期 ...
- rust学习笔记(4)
流程控制 if if n < 0 { print!("{} is negative", n); } else if n > 0 { print!("{} is ...
- Ubuntu Nvidia driver驱动安装及卸载
前言 当前英伟达下载的驱动不再是 .run 的 shell文件,所以有了新的文档,如下 Ubuntu Nvidia driver驱动安装(新) 当然如果你有 shell 文件,也可以继续使用本文档安装 ...
- mysql frm、MYD、MYI数据文件恢复,导入MySQL中
前言 .frm..MYI..MYD 文件分别是 MySQL 的 MyISAM存储引擎存储的表结构.索引.数据文件. 简单方法恢复数据 .frm..MYI..MYD文件如果直接以文本打开,全部会以二进制 ...
- Delphi 数据库连接查询分析器
为了方便自己查询数据库信息,花了小时间写了小工具,添加SQL语法高亮显示功能
- 使用Python建立双缝干涉模型
引言 双缝干涉实验是物理学中经典的实验之一,它展示了光的波动性以及量子力学的奇异性.实验结果表明,当光或粒子通过两条狭缝时,它们会产生干涉现象,形成明暗相间的条纹图案.这种现象不仅说明了光的波动性,还 ...
- 20241105,LeetCode 每日一题,用 Go 实现两数之和的非暴力解法
题目 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案,并且你不能 ...