简体   繁体   English

将代码动态附加到方法 .Net-Core

[英]Dynamically Append Code to a method .Net-Core

I'm interested in dynamically appending code in .Net-Core.我对在 .Net-Core 中动态附加代码很感兴趣。 Note: This is for education purposes only.注意:这仅用于教育目的。

Currently I have a class which swaps methods:目前我有一个交换方法的类:

public static void Inject<TTarget, TInject>(string targetFuncName, string injectFuncName)
{
    MethodInfo methodToReplace = typeof(TTarget).GetMethod(targetFuncName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
    MethodInfo methodToInject = typeof(TInject).GetMethod(injectFuncName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
    RuntimeHelpers.PrepareMethod(methodToReplace.MethodHandle);
    RuntimeHelpers.PrepareMethod(methodToInject.MethodHandle);

    unsafe
    {
        if (IntPtr.Size == 4)
        {
            int* inj = (int*)methodToInject.MethodHandle.Value.ToPointer() + 2;
            int* tar = (int*)methodToReplace.MethodHandle.Value.ToPointer() + 2;
#if DEBUG
            Console.WriteLine("\nVersion x86 Debug\n");

            byte* injInst = (byte*)*inj;
            byte* tarInst = (byte*)*tar;

            int* injSrc = (int*)(injInst + 1);
            int* tarSrc = (int*)(tarInst + 1);

            *tarSrc = (((int)injInst + 5) + *injSrc) - ((int)tarInst + 5);
#else
            Console.WriteLine("\nVersion x86 Release\n");
            *tar = *inj;
#endif
        }
        else
        {
            long* inj = (long*)methodToInject.MethodHandle.Value.ToPointer() + 1;
            long* tar = (long*)methodToReplace.MethodHandle.Value.ToPointer() + 1;
#if DEBUG
            Console.WriteLine("\nVersion x64 Debug\n");
            byte* injInst = (byte*)*inj;
            byte* tarInst = (byte*)*tar;

            int* injSrc = (int*)(injInst + 1);
            int* tarSrc = (int*)(tarInst + 1);

            *tarSrc = (((int)injInst + 5) + *injSrc) - ((int)tarInst + 5);
 #else
            Console.WriteLine("\nVersion x64 Release\n");
            *tar = *inj;
#endif
        }
    }
}

This Code swaps the method fine, However, if you're debugging it seems like to original code is never hit.此代码很好地交换了方法,但是,如果您正在调试,则似乎永远不会命中原始代码。 Instead I would like to swap the return statements from the method bytes and replace it with a jump statement to another function with the same parameters.相反,我想交换方法字节中的返回语句,并将其替换为跳转到具有相同参数的另一个函数的语句。

However, .Net Core doesn't currently support the MethodRental.SwapMethodBody How Can dynamically append code to the end of a function?但是,.Net Core 目前不支持MethodRental.SwapMethodBody如何将代码动态附加到函数的末尾?

I figured out a cheat way to do it.我想出了一个作弊的方法来做到这一点。 I haven't written all the code yet but I'll explain in pseudo code我还没有写完所有的代码,但我会用伪代码来解释

Say we have a method:假设我们有一个方法:

public class ValuesController: ControllerBase
{
    [HttpGet("Seven")]
    public int Seven(string id)
    {
        return 7;
    }
}

We want to swap it with another method我们想用另一种方法交换它

public int Eight(int retValue)
{
    return retValue + 1;
}

We can generate a Func in a dynamic assembly.我们可以在动态程序集中生成一个 Func。 Which looks like so:看起来像这样:

public int Dummy(string id)
{}

public int DummyFuncContainer(string id)
{
    var result = Dummy(id);
    return Eight(result);
}

Then all you need to do is.然后你需要做的就是。

Swap the Dummy() method with the func we want Seven() and then we swap Seven() (Which is now pointing at Dummy() ) with DummyFuncContainer() .用我们想要的函数Seven()交换Dummy()方法,然后用DummyFuncContainer()交换Seven() (现在指向Dummy() DummyFuncContainer()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM