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