我想将代码注入到外部方法中,而无需编辑源代码。 由于Dynamically replace the contents of a C# method?,我已经可以替换当前方法以引用自己的方法。
我仍然需要能够在运行时修改外部方法主体,这意味着能够在方法调用之前和方法调用之后注入代码。
我不能 手动调用我要修改的此方法。是本身运行的服务。
我尝试过:
Marshal.GetFunctionPointerForDelegate
,然后在调用marshalled函数之后将方法替换为->不起作用,因为replace是指针,而marhal更改了指针。情况:
class Program
{
static void Main(string[] args)
{
var oldMethod = typeof(InstanceClassA).GetMethod("OldMethod", BindingFlags.Instance | BindingFlags.Public);
var beforeMethod = typeof(InstanceClassA).GetMethod("BeforeMethod", BindingFlags.Instance | BindingFlags.Public);
oldMethod.Before(() => Console.WriteLine("Called Before"));
oldMethod.Before(() => beforeMethod);
//This is *TESTING* only as I can't manually call the method i want to inject.
var instance = new InstanceClassA();
//Should now call the beforeMethod and the called before
instance.OldMethod("With Before");
//Should call the after method
Console.ReadLine();
}
}
public static class MethodInfoUtils
{
//Will *NOT* allow return values as this should return oldMethod return value
//Will allow Actions and another MethodInfo
public static void Before(this MethodInfo method)
{
// Code to inject code before calling the external method
}
//Will *NOT* allow return values as this should return oldMethod return value
//Will allow Actions and another MethodInfo
public static void After(this MethodInfo method)
{
// Code to inject code after calling the external method
}
}
public class InstanceClassA
{
public bool OldMethod(string message)
{
Console.WriteLine(message);
return true;
}
//message param is equal to the oldMethod param as it is called before the oldMethod is called
public void BeforeMethod(string message)
{
Console.WriteLine("test");
}
}