【发布时间】:2017-08-31 03:02:47
【问题描述】:
我想在.Net Framework 4中用动态方法替换一个方法,然后我在Dynamically replace the contents of a C# method?找到了一个非常有用的答案,但是我无法直接从DynamicMethod获取MethodHandle:
我们无法返回 MethodHandle,因为我们无法通过 GC 对其进行跟踪,因此此方法不受限制
本文CLR Injection: Runtime Method Replacer,
private static IntPtr GetDynamicMethodRuntimeHandle(MethodBase method)
{
if (method is DynamicMethod)
{
FieldInfo fieldInfo = typeof(DynamicMethod).GetField("m_method",
BindingFlags.NonPublic|BindingFlags.Instance);
return ((RuntimeMethodHandle)fieldInfo.GetValue(method)).Value;
}
return method.MethodHandle.Value;
}
找不到哪个m_method。
然后我注意到m_methodHandle,但不知道什么时候会初始化。
internal unsafe RuntimeMethodHandle GetMethodDescriptor() {
if (m_methodHandle == null) {
lock (this) {
if (m_methodHandle == null) {
if (m_DynamicILInfo != null)
m_DynamicILInfo.GetCallableMethod(m_module, this);
else {
if (m_ilGenerator == null || m_ilGenerator.ILOffset == 0)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_BadEmptyMethodBody", Name));
m_ilGenerator.GetCallableMethod(m_module, this);
}
}
}
}
return new RuntimeMethodHandle(m_methodHandle);
}
根据另一个问题Resolving the tokens found in the IL from a dynamic method,DynamicResolver 有一个ResolveToken 方法,它返回methodHandle 地址。所以我在答案中使用了一些代码:
var resolver = typeof(DynamicMethod)
.GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(dynamicMethod);
if (resolver == null)
throw new ArgumentException("The dynamic method's IL has not been finalized.");
但是... DynamicResolver 只会在DynamicILGenerator.GetCallableMethod 方法中被初始化,该方法将在DynamicMethod.GetMethodDescriptor 方法中调用,所以当我得到它时resolver 必须为空。
这是我的动态方法:
private static MethodInfo build(MethodInfo originMethod)
{
var parameters = originMethod.GetParameters();
var parameterTypes = parameters.Length == 0 ?
null :
parameters
.Select(param => param.ParameterType)
.ToArray();
DynamicMethod method = new DynamicMethod(
originMethod.Name,
originMethod.ReturnType,
parameterTypes,
originMethod.Module);
ILGenerator il = method.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Injected");
var console_writeline = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
il.Emit(OpCodes.Call, console_writeline);
il.Emit(OpCodes.Ret);
return method;
}
JIT 学的很少,不太懂。
有人可以帮忙吗?
--------------------------------已编辑-------------- ------------
@Latency 的答案很好用:
RuntimeMethodHandle GetMethodRuntimeHandle(MethodBase method)
{
if (!(method is DynamicMethod))
return method.MethodHandle;
RuntimeMethodHandle handle;
if (Environment.Version.Major == 4)
{
var getMethodDescriptorInfo = typeof(DynamicMethod).GetMethod("GetMethodDescriptor", BindingFlags.NonPublic | BindingFlags.Instance);
handle = (RuntimeMethodHandle)getMethodDescriptorInfo.Invoke(method, null);
}
else
{
var fieldInfo = typeof(DynamicMethod).GetField("m_method", BindingFlags.NonPublic | BindingFlags.Instance);
handle = (RuntimeMethodHandle)fieldInfo.GetValue(method);
}
return handle;
}
过了这么久,我不记得在获取 RuntimeMethodHandle 并拒绝动态方法之后接下来会发生什么,但我希望这可以帮助其他人。
【问题讨论】:
标签: c# dynamicmethod