Search code examples
c#.netdependency-injectionservice-provider

What is the Microsoft.Practices.Unity ResolverOverride equivalent in Microsoft.Extensions.DependencyInjection


I am trying to update my dependency injection to use Microsoft's built in dependency injection rather than using the deprecated Unity dependency injection.

Unity has the following resolve: Resolve(Type t, string name, params ResolverOverride[] resolverOverrides);

I was trying to do the same thing with a ServiceProvider, but there is no such function. Is there a Service Provider function that does this?

After doing some research I found that creating an instance using ActivatorUtilities could be the fix to my problem, however I need help with the object[] parameter. I need one that is similar if not exactly the same as ResolverOverride, but I no longer want to use the Unity package. Is there a class that acts exactly like this?

If not, where can I find the source code of ParameterOverride so I can make the class myself. Using Google, I could only find the Microsoft Documentation but it doesn't contain any useful information


Solution

  • For those who stumble upon this post, I solved it by just making my own function.

    If want to know what my function looked like:

    My custom function takes in the implementation type, and a Map with the key being the parameter name and the value being the value of the parameter. The function looks through all of the constructors and picks the constructor that best matches the parameters passed in. To get the implementation type given the service type, you can use the service collection and call something like collection.FirstOrDefault(...)