免责声明:我这样做是出于学习目的。这不会在代码中使用。
我试图了解泛型的方法表是如何结构的,我想在运行时动态附加到方法。我发现一个非常有用的stack overflow question让我开始的引用。
我有一个简单的 Controller ,我用它作为测试来验证我的方法正在交换:
public class ValuesController : ControllerBase
{
static ValuesController() {
var methodToReplace = typeof(ValuesController).GetMethod(nameof(ValuesController.Seven),
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var methodToAppend = typeof(ValuesController).GetMethod(nameof(ValuesController.Eight),
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
new Initializer(methodToReplace, methodToAppend);
}
[HttpGet("Seven")]
public int Seven(string id)
{
return 7;
}
[HttpGet("Eight")]
public int Eight(string id)
{
return 8;
}
}
我有课Initializer
它负责处理附加到方法的操作。
public class Initializer
{
public Initializer(MethodInfo methodToReplace, MethodInfo methodToAppend)
{
var dummyMethod = typeof(Initializer).GetMethod(nameof(Dummy),
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var proxyMethod = typeof(Initializer).GetMethod(nameof(Proxy),
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
var appendedMethod = typeof(Initializer).GetMethod(nameof(Appended),
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
dummyMethod.OneWayReplace(methodToReplace);
methodToReplace.OneWayReplace(proxyMethod);
appendedMethod.OneWayReplace(methodToAppend);
}
public int Proxy(string id)
{
Dummy(id);
return Appended(id);
}
public int Dummy(string id)
{
return 0;
}
public int Appended(string id)
{
return 0;
}
}
然后我有了从原始 stackoverflow 问题中获得的扩展:
public static class InjectionExtensions
{
// Note: This method replaces methodToReplace with methodToInject
// Note: methodToInject will still remain pointing to the same location
public static unsafe MethodReplacementState OneWayReplace(this MethodInfo methodToReplace, MethodInfo methodToInject)
{
//#if DEBUG
RuntimeHelpers.PrepareMethod(methodToReplace.MethodHandle);
RuntimeHelpers.PrepareMethod(methodToInject.MethodHandle);
//#endif
MethodReplacementState state;
IntPtr tar = methodToReplace.MethodHandle.Value;
var inj = methodToInject.MethodHandle.Value + 8;
if (!methodToReplace.IsVirtual)
tar += 8;
else
{
var index = (int)(((*(long*)tar) >> 32) & 0xFF);
var classStart = *(IntPtr*)(methodToReplace.DeclaringType.TypeHandle.Value + (IntPtr.Size == 4 ? 40 : 64));
tar = classStart + IntPtr.Size * index;
}
#if DEBUG
tar = *(IntPtr*)tar + 1;
inj = *(IntPtr*)inj + 1;
state.Location = tar;
state.OriginalValue = new IntPtr(*(int*)tar);
*(int*)tar = *(int*)inj + (int)(long)inj - (int)(long)tar;
return state;
#else
state.Location = tar;
state.OriginalValue = *(IntPtr*)tar;
* (IntPtr*)tar = *(IntPtr*)inj;
return state;
#endif
}
}
注意:使用当前设置一切正常。但是,第二次我更改 Initializer
类成为泛型类 Initializer<T>
我遇到内存违规:
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
我的猜测是 methodToReplace.DeclaringType.TypeHandle.Value
泛型的计算有所不同,或者因为编译器是生成泛型类的人,所以将其写入 protected 内存?
编辑 我发现了在使用通用参数时正确准备方法所需的更多信息,例如:
RuntimeHelpers.PrepareMethod(methodToInject.MethodHandle, new[] { typeof(T).TypeHandle });
但是,要实现这一目标,还需要解决一些难题。
编辑
有一些开源项目,例如 harmony做类似的事情,但是看起来他们发出自己的程序集。虽然我考虑过这个选项,但我仍然更愿意了解方法表如何与泛型一起使用
如何附加到驻留在泛型类中的方法?
最佳答案
我想您已经看到了:Dynamically replace the contents of a C# method?
我在自己的项目中采用了其中一些方法@ https://github.com/juliusfriedman/net7mma_core/blob/master/Concepts/Classes/MethodHelper.cs
我认为问题是,如果您正在使用附加的调试器运行,那么您还需要在编译时处理当前由 IFDEF
定义的逻辑部分,并将其替换为 System.Diagnostics.Debugger.IsAttached
尽管偏移量计算(以跳过调试器注入(inject)的代码)可能需要根据各种因素(例如所使用的框架版本)进行更改。
参见https://github.com/juliusfriedman/net7mma_core/blob/master/Concepts/Classes/MethodHelper.cs#L35
当未附加调试器并且我在 Release模式下运行时,这在 .Net Core 3.1 中适用于我;在附加或不附加调试器的 Debug模式下运行时,或者在附加调试器的 Release模式下运行时,我会收到不同的异常。 (在调试中我收到算术溢出,而在发布中我收到执行引擎异常)。
此外,这只在 JIT 分层启动之前有效,如果我在没有附加调试器的情况下第二次运行该方法,则会收到内部 CLR 错误。
我相信这与调试器在附加时注入(inject)的代码有关,说实话,我并不了解调试器在附加时注入(inject)的确切内容。
我会制作一个简化的问题仓库并提出一个问题@ https://github.com/dotnet/runtime如果您需要它与附加的调试器一起使用,我相信那里会有人指导您正确的方向。
关于c# - 内存违规在运行时动态附加到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59778004/