我想将代码注入外部方法,而不编辑源代码。我已经可以替换当前方法来引用我自己的方法,因为动态替换 C# 方法的内容?.
我仍然需要能够在运行时修改外部方法体,这意味着能够在方法调用之前和方法调用之后注入代码。
我不能 手动调用我想修改的这个方法。是一个自己运行的服务。
我努力了:
- Reflection.Emit -> 但这会创建一个未被引用的新方法。
- Marshall ->
Marshal.GetFunctionPointerForDelegate
,然后将方法替换为之后调用编组函数 -> 不起作用,因为替换是指针并且 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");
}
}