- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想做的是改变 C# 方法在被调用时的执行方式,这样我就可以这样写:
[Distributed]
public DTask<bool> Solve(int n, DEvent<bool> callback)
{
for (int m = 2; m < n - 1; m += 1)
if (m % n == 0)
return false;
return true;
}
在运行时,我需要能够分析具有分布式属性的方法(我已经可以做到),然后在函数主体执行之前和函数返回之后插入代码。更重要的是,我需要能够在不修改调用 Solve 的代码或在函数开始时(在编译时;在运行时这样做是目标)的情况下执行此操作。
目前我尝试了这段代码(假设t是Solve存储的类型,m是Solve的MethodInfo):
private void WrapMethod(Type t, MethodInfo m)
{
// Generate ILasm for delegate.
byte[] il = typeof(Dpm).GetMethod("ReplacedSolve").GetMethodBody().GetILAsByteArray();
// Pin the bytes in the garbage collection.
GCHandle h = GCHandle.Alloc((object)il, GCHandleType.Pinned);
IntPtr addr = h.AddrOfPinnedObject();
int size = il.Length;
// Swap the method.
MethodRental.SwapMethodBody(t, m.MetadataToken, addr, size, MethodRental.JitImmediate);
}
public DTask<bool> ReplacedSolve(int n, DEvent<bool> callback)
{
Console.WriteLine("This was executed instead!");
return true;
}
但是,MethodRental.SwapMethodBody 仅适用于动态模块;不是那些已经编译并存储在程序集中的。
所以我正在寻找一种方法来有效地对已存储在加载和执行程序集中的方法执行 SwapMethodBody。
请注意,如果我必须将方法完全复制到动态模块中,这不是问题,但在这种情况下,我需要找到一种方法来跨 IL 复制并更新对 Solve() 的所有调用这样他们就会指向新副本。
最佳答案
Disclosure: Harmony is a library that was written and is maintained by me, the author of this post.
Harmony 2是一个开源库(MIT 许可),旨在在运行时替换、修饰或修改任何类型的现有 C# 方法。它主要关注用 Mono 或 .NET 编写的游戏和插件。它负责对同一方法进行多次更改 - 它们会累积而不是相互覆盖。
它为每个原始方法创建动态替换方法,并向它们发出代码,在开始和结束时调用自定义方法。它还允许您编写过滤器来处理原始 IL 代码和自定义异常处理程序,从而允许对原始方法进行更详细的操作。
为了完成这个过程,它将一个简单的汇编器跳转到原始方法的蹦床中,指向从编译动态方法生成的汇编器。这适用于 Windows、macOS 和 Mono 支持的任何 Linux 上的 32/64 位。
可以找到文档here .
( Source )
原始代码
public class SomeGameClass
{
private bool isRunning;
private int counter;
private int DoSomething()
{
if (isRunning)
{
counter++;
return counter * 10;
}
}
}
使用 Harmony 注释进行修补
using SomeGame;
using HarmonyLib;
public class MyPatcher
{
// make sure DoPatching() is called at start either by
// the mod loader or by your injector
public static void DoPatching()
{
var harmony = new Harmony("com.example.patch");
harmony.PatchAll();
}
}
[HarmonyPatch(typeof(SomeGameClass))]
[HarmonyPatch("DoSomething")]
class Patch01
{
static FieldRef<SomeGameClass,bool> isRunningRef =
AccessTools.FieldRefAccess<SomeGameClass, bool>("isRunning");
static bool Prefix(SomeGameClass __instance, ref int ___counter)
{
isRunningRef(__instance) = true;
if (___counter > 100)
return false;
___counter = 0;
return true;
}
static void Postfix(ref int __result)
{
__result *= 2;
}
}
或者,使用反射手动修补
using SomeGame;
using System.Reflection;
using HarmonyLib;
public class MyPatcher
{
// make sure DoPatching() is called at start either by
// the mod loader or by your injector
public static void DoPatching()
{
var harmony = new Harmony("com.example.patch");
var mOriginal = typeof(SomeGameClass).GetMethod("DoSomething", BindingFlags.Instance | BindingFlags.NonPublic);
var mPrefix = typeof(MyPatcher).GetMethod("MyPrefix", BindingFlags.Static | BindingFlags.Public);
var mPostfix = typeof(MyPatcher).GetMethod("MyPostfix", BindingFlags.Static | BindingFlags.Public);
// add null checks here
harmony.Patch(mOriginal, new HarmonyMethod(mPrefix), new HarmonyMethod(mPostfix));
}
public static void MyPrefix()
{
// ...
}
public static void MyPostfix()
{
// ...
}
}
关于c# - 动态替换 C# 方法的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7299097/
我想对一个字符串执行搜索和替换,比如 password。 正如您从问题中了解到的那样,替换后的字符串应变为 sdvvzrug。 但不幸的是,下面的代码输出bbbbcaab: $search = ran
我正在使用 futurize --stage2它应用了许多源代码转换以使代码 python2 和 python3 兼容。其中一个修复是所有分区 a/b 都替换为 old_div(a/b),我想避免这种
我正在使用 RStudio,但我在控制台上的输出被截断了。我找不到如何停止截断(我尝试搜索 ?options 以及在谷歌上搜索的时间比我想承认的要长)。 编辑:我向大家道歉!我最初的长名称为“This
我有一个 fragment 堆栈,我在其中使用替换和相加。添加或替换我的 fragment 的代码(在我的 Activity 中)如下 private fun addFragment(fragment
我在一个数组中插入了一些字符串,但在我这样做之前,我想按照主题所说的去做。只用 %20 替换空格,我这样做: Name.push(linkText.replace(" ", "%20")); 但是我如
我正在尝试编译和测试我在网上看到的代码 Expanding an IP add 。但是,当我尝试编译它时,我收到有关 StringBuilder 替换方法的错误。它说: IPadd.java:52:
我正在尝试使用 dplyr 的最新功能重写我的部分代码,方法是将 data.frame() 替换为 data_frame() 和 cbind() 与 bind_cols(): library(rgeo
我最近偶然发现了 replace()和 "[ x.tst s.tst s.tst [,1] [,2] [,3] [1,] 0 0 0
我一直想知道,如何在给定的参数内进行替换。 如果你有这样的一行: 123,Hello,World,(I am, here), unknown 你想更换 World与 Foobar那么这是一个简单的任务
如何转义字符串中的双引号?例如, input: "Nobody" output: \"Nobody\" 我尝试过这样的操作,但不起作用: String name = "Nobody"; name.r
我正在做类似的事情: SQL sql sQl SqL var ps = document.getElementsByTagName('p'); for(var i = 0; i 但它不会替换文本。
我正在尝试用 \" 替换所有 " 并用 JSON 解析字符串,但浏览器抛出错误 SyntaxError: JSON Parse error: Unrecognized token '\'. 下面是代码
大家好,在这里挣扎...... 是否可以将第一个正斜杠之间的任何内容替换为“”,但保留其余部分? 例如var 将是 string "/anything-here-this-needs-to-be-re
在下面的代码中,JavaScript 替换函数中的 alert(a) 将提醒匹配的字符串,在本例中,将是 {name} 和 {place}。 这按照文档 javascript docs 的描述工作,即
+-----------------------------+ | tables | +-------------------
我正在尝试用\"替换包含 "的字符串,下面是我尝试过的程序 String s="\"/test /string\""; s = s.replaceAll("\"", "\\\"");
var text = "a's ..a's ...\"... "; text = convert(text); function convert( text ) { var n = text
我正在尝试使用 JavaScript 中的替换函数,但有一个问题。 strNewDdlVolCannRegion = strNewDdlVolCannRegion.replace(/_existing
好吧,首先我对我的上一篇文章感到非常抱歉,但我真的需要帮助,我会把我真正想要的东西放在一个更清晰的代码中。我不擅长 javascript,所以希望你能帮助我。
我正在写一张纸条,遇到了障碍。可能有更有效的方法来执行此操作,但我对 Python 还很陌生。我正在尝试创建用户生成的 IP 地址列表。我正在使用 print 来查看生成的值是否正确。当我运行此代码时
我是一名优秀的程序员,十分优秀!