简体   繁体   中英

Fast dynamic invoke between assemblies

Invoker.csproj -> Program.cs

namespace InvokeLibrary
{
    public class Invoke
    {
        public static void Invoker(Delegate method, params object[] data)
        {
            for (int i = 0; i < 1000000; i++)
                method.DynamicInvoke(data);
        }
    }
}

Caller.csproj -> Program.cs

using InvokeLibrary;

object obj = new A("Message from caller");
Invoke.Invoker(ToBeInvoked, obj);

void ToBeInvoked(A obj) =>
    Console.WriteLine("A: " + obj.Message);
record A(string Message);

What would be the most efficient way for the library to invoke given delegates?

The library will call the same methods multiple times and the given data will always match the function's parameters.
Is there a way to improve performance or make the call less 'dynamic' trading for a slow overhead? Functions will be called many times so I don't mind if the first invoke will be slow.
I know about MethodInfo.Invoke but it takes too much memory to store in a list and Delegate.DynamicInvoke is too slow for my requirements

Check out the EfficientInvoker implementation from Tom Dupont which he claims is 10 times faster than Delegate.DynamicInvoke

GitHub path: https://github.com/tdupont750/tact.net/blob/master/framework/src/Tact/Reflection/EfficientInvoker.cs

The Tests class contains examples of usage: https://github.com/tdupont750/tact.net/blob/master/framework/tests/Tact.Tests/Reflection/EfficientInvokerTests.cs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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