0

I'm writing a framework which aids in making rest dynamic. I have a factory which build my rest services, after the factory is built I need to register events for my websockets.

I do not have my concrete hubtype at compile time so I'm using reflection to create an action to invoke my Generic Method.

static Action<IServiceProvider, IAsyncCRUDService<TDTO>> DynamicallyRegisterHubEvents<TDTO>(Type hubType)
{
    var registerServiceMethod = RegisterHubEventsMethodInfo.MakeGenericMethod(typeof(TDTO), hubType);

    return (IServiceProvider services, IAsyncCRUDService<TDTO> restService) =>
    {
        var parameters = new object[] { services, restService };
        registerServiceMethod.Invoke(null, parameters);
    };
}

The result of this action will be used inside of the factory which creates my service.

We all know reflection is slow.

But since, I've abstracted the reflection portion outside of the action, my method info will be captured with a closure. Will this still be slow? Or Is there a more efficient to do this?

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 2
    You are still executing the `MethodInf.Invoke(...)` inside the returned action, which does use Reflection. How often do you expect it to be executed? You might consider using `dynamic` which automatically caches things for you, so you wouldn't have a bad performance *after the first run*. You could try to use delegates, check this other question: https://stackoverflow.com/questions/10313979/methodinfo-invoke-performance-issue – IPValverde Feb 11 '18 at 16:45
  • It’s in a factory for my dependency injection container so every request. I had it working a different way but I wanted to reuse this code lag instead of maintaining two, welp looks like I’ll just roll back – johnny 5 Feb 11 '18 at 17:10
  • @IPValverde Thanks I saw an Immediate performance gain when I switch to wrap the internal function in an action instead of returning the action that called invoke – johnny 5 Feb 11 '18 at 18:32

0 Answers0