lundi 29 juillet 2019

Passing type as a parameter in an api that takes a generic type

I can see many similar questions, but I can't find a solution covering this case:

I have the following code:

Service.AddQuery(T => new ServiceFactory.New())
    .AddQueryType<Test>()
    .Register(T)
    .Create());


public class Test
{
    ...
}

it's not my code and it can't be changed.

Since the system has a plugin architecture, I cannot know the Test type ahead of time, so I need to do something like:

RegisterService(Type MyQueryType)
{
    Service.AddQuery(T => new ServiceFactory.New())
        .AddQueryType<MyQueryType>()
        .Register(T)
        .Create());
}

but I can't find how to make this work.

I tried to pass typeof, I tried to create an instance with Activator.CreateInstance, etc and I can't find how to make this happen.

How can this be done?





Aucun commentaire:

Enregistrer un commentaire