C# 运行时替换方法(需要unsafe编译)
/* https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method
For .NET 4 and above "C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe" /unsafe+ /out:replacemethod.exe replacemethod.cs && start "replacemethod.exe" replacemethod.exe
PrintInfo: Version x64 Release 1. Replace Instance Method Class1.Demo()
Demo
Replace result: True
Foo 2. Replace Instance Overload of Method Class1.Demo(10)
Demo: 10
Replace result: True
Foo: 10 3. Replace Static Method Class1.DemoStatic()
DemoStatic
Replace result: True
FooStatic Press any key to EXIT...
*/ using System;
using System.Reflection;
using System.Runtime.CompilerServices; class Program {
public static void Main(params string[] args){
PrintInfo();
Console.WriteLine(); Test(); Console.Write("\nPress any key to EXIT...");
Console.ReadKey(true);
} public static void PrintInfo() {
Console.Write("PrintInfo: ");
if (IntPtr.Size == ) {
#if DEBUG
Console.WriteLine("Version x86 Debug");
#else
Console.WriteLine("Version x86 Release");
#endif
} else {
#if DEBUG
Console.WriteLine("Version x64 Debug");
#else
Console.WriteLine("Version x64 Release");
#endif
}
} public static void Test() {
Console.WriteLine("1. Replace Instance Method Class1.Demo()");
var o = new Class1();
o.Demo();
var r = CSharpUtils.ReplaceMethod(typeof(Class1), "Demo", typeof(Program), "Foo");
Console.WriteLine("Replace result: {0}", r);
o.Demo(); Console.WriteLine(); Console.WriteLine("2. Replace Instance Overload of Method Class1.Demo(10)");
o.Demo();
r = CSharpUtils.ReplaceMethod(typeof(Class1), "Demo", typeof(Program), "Foo", types:new Type[]{ typeof(int) });
Console.WriteLine("Replace result: {0}" ,r);
o.Demo(); Console.WriteLine(); Console.WriteLine("3. Replace Static Method Class1.DemoStatic()");
Class1.DemoStatic();
r = CSharpUtils.ReplaceMethod(typeof(Class1), "DemoStatic", typeof(Program), "FooStatic", BindingFlags.Static|BindingFlags.NonPublic|BindingFlags.Public);
Console.WriteLine("Replace result: {0}" ,r);
Class1.DemoStatic();
} private void Foo() {
Console.WriteLine("Foo");
} private void Foo(int d) {
Console.WriteLine("Foo: {0}", d);
} private static void FooStatic() {
Console.WriteLine("FooStatic");
}
} class Class1 {
public void Demo() {
Console.WriteLine("Demo");
} public void Demo(int d) {
Console.WriteLine("Demo: {0}", d);
} public static void DemoStatic() {
Console.WriteLine("DemoStatic");
}
} public static class CSharpUtils {
public static bool ReplaceMethod(Type targetType, string targetMethod, Type injectType, string injectMethod, BindingFlags bindingAttr = BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public, Binder binder = null, CallingConventions callConvention = CallingConventions.Any, Type[] types = null, ParameterModifier[] modifiers = null) {
if (types == null) {
types = Type.EmptyTypes;
}
MethodInfo tarMethod = targetType.GetMethod(targetMethod, bindingAttr, binder, callConvention, types, modifiers);
MethodInfo injMethod = injectType.GetMethod(injectMethod, bindingAttr, binder, callConvention, types, modifiers);
if (tarMethod == null || injMethod == null) {
return false;
}
RuntimeHelpers.PrepareMethod(tarMethod.MethodHandle);
RuntimeHelpers.PrepareMethod(injMethod.MethodHandle);
unsafe {
if (IntPtr.Size == ) {
int* tar = (int*)tarMethod.MethodHandle.Value.ToPointer() + ;
int* inj = (int*)injMethod.MethodHandle.Value.ToPointer() + ;
*tar = *inj;
} else {
long* tar = (long*)tarMethod.MethodHandle.Value.ToPointer() + ;
long* inj = (long*)injMethod.MethodHandle.Value.ToPointer() + ;
*tar = *inj;
}
}
return true;
}
}
C# 运行时替换方法(需要unsafe编译)的更多相关文章
- 1.2 - C#语言习惯 - 用运行时常量readonly而不是编译期常量const
C#中有两种类型的常量:编译期常量和运行时常量.二者有着截然不同的行为,使用不当将会带来性能上或正确性上的问题. 这两个问题最好都不要发生,不过若难以同时避免的话,那么一个略微慢一些但能保证正确的程序 ...
- gohook 一个支持运行时替换 golang 函数的库实现
运行时替换函数对 golang 这类静态语言来说并不是件容易的事情,语言层面的不支持导致只能从机器码层面做些奇怪 hack,往往艰难,但如能成功,那挣脱牢笼带来的成就感,想想就让人兴奋. gohook ...
- Objective-C运行时编程 - 方法混写 Method Swizzling
摘要: 本文描述方法混写对实例.类.父类.不存在的方法等情况处理,属于Objective-C(oc)运行时(runtime)编程范围. 编程环境:Xcode 6.1.1, Yosemite,iOS 8 ...
- OC运行时和方法机制笔记
在OC当中,属性是对字段的一种特殊封装手段. 在编译期,编译器会将对字段的访问替换为内存偏移量,实质是一种硬编码. 如果增加一个字段,那么对象的内存排布就会改变,需要重新编译才行. OC的做法是,把实 ...
- 趣谈iOS运行时的方法调用原理
一个成熟的计算机语言必然有丰富的体系,复杂的容错机制,处理逻辑以及判断逻辑.但这些复杂的逻辑都是围绕一个主线丰富和展开的,所以在学习计算机语言的时候,先掌握核心,然后了解其原理,明白程序语言设计的实质 ...
- 《C#高效编程》读书笔记02-用运行时常量(readonly)而不是编译期常量(const)
C#有两种类型的常量:编译期常量和运行时常量.两者有截然不同的行为,使用不当的话,会造成性能问题,如果没法确定,则使用慢点,但能保证正确的运行时常量. 运行时常量使用readonly关键字声明,编译期 ...
- MEF在运行时替换插件
利用AppDomain的ShadowCopy特性. var setup = new AppDomainSetup { CachePath = cachePath, ShadowCopyFiles = ...
- c# 运行时替换某文件源代码(将XML 转换成 某个枚举并写入源文件)
var sr = new StreamReader(Server.MapPath("~/WEB-INF/rule.config")); XmlDocument doc = new ...
- java 利用java运行时的方法得到当前屏幕截图的方法(转)
将截屏图片保存到本地路径: package com.test; import java.awt.AWTException; import java.awt.Dimension; import java ...
随机推荐
- Request.RawUrl、Request.Url的区别
如果访问的地址是: http://hovertree.com/guestbook/addmessage.aspx?key=hovertree%3C&n=myslider#zonemenu 那么 ...
- appium入门基础知识
1.概念区分: 1)IOS-UIAutomation:随着iOS4.0的发布,苹果公司同时发布了一个名为UIAutomation的测试框架,它可以用来在真实设备和iPhone模拟器上执行自动化测试 学 ...
- Python3_实例汇总
1.Python数字求和 # -*- codingLuft-8 -*- #Filename: test.py #author by:Leq #用户输入数字 num1 = input("输入第 ...
- HDU 6319 Ascending Rating (单调双端队列)
题意:给定一个序列a[1..n],对于每个长度为m的连续子区间,求出区间的最大值和从左往右扫描该区间最大值的变化次数. 分析:先O(n)处理出整个序列的值.求出每个长度为m的连续区间中的最大值可以用单 ...
- rabbitmq的发布确认和事务
摘要: 介绍confirm的工作机制.使用spring-amqp介绍事务以及发布确认的使用方式.因为事务以及发布确认是针对channel来讲,所以在一个连接中两个channel,一个channel可以 ...
- sql 语句 名称解析,是 由内向外的。
子查询内 找不到的 字段 会 向外 寻找,还是找不到 就报错:找到了就不报错,但是 子查询语句就毫无意义了: 解决办法: 字段前面要跟上表的名称. 一般 字段无效 立刻 报错.
- python中多重继承与获取对象
1.python中多重继承 除了从一个父类继承外,Python允许从多个父类继承,称为多重继承. 多重继承的继承链就不是一棵树了,它像这样: class A(object): def __init__ ...
- JMS、MQ、ActiveMQ
JMS 的一个标准或者说是一个协议. 通常用于企业级应用的消息传递. 主要有topic 消息(1 对多), queue 消息(1对1). ActiveMQ 是一个JMS 的实现, apache 出的. ...
- Percona 工具包 pt-online-schema-change 简介
mysql的在线表结构修改,因为低效和阻塞读写.一直被诟病.至于ALTER TABLE 的原理,参看我上一篇文章.MySQL在线修改大表结构.看完后,发现的问题是还是会锁的,且对于在线更新的这块也是不 ...
- IOS 出现错误 :Reason: image not found
把Build Phases 里HyphenateLite.framework后边的选项修改成为Optional就可以了 dyld 后面有提示 HyphenateLite.framework