C
C#2y ago
Thinker

Creating an instance of a class using DI [Answered]

This is purely just out of curiosity. If you have a class which takes some arguments in its ctor and you want to create an instance of the class using some types registered in DI, is there any simple way to do that?
13 Replies
Pobiega
Pobiega2y ago
Is the class you are wanting to create also registered? like, you want to create a class A instance, and has a ctor like A(B b, C c)
Thinker
Thinker2y ago
B and C are registered in DI and I want to create an instance of A using the registered types.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Thinker
Thinker2y ago
Of course I could just get the B and C from DI and create it manually, but I was just wondering if there was some automatic way.
Pobiega
Pobiega2y ago
if A is not registered, you just get B and C out but ideally, A would be regged is A registered or not?
Thinker
Thinker2y ago
Again this is just a hypothetical so doesn't really matter, sure it could be.
Pobiega
Pobiega2y ago
if it is, you just resolve A directly and the DI container will internally resolve B and C for you
Thinker
Thinker2y ago
Right Was probably just wondering if there was a way to resolve A without registering it.
Pobiega
Pobiega2y ago
like, you'd do something along the lines of... serviceProvider.Resolve<A>(); and it would look at A and see if it could resolve all the dependencies and in that case do so? afaik, not possible and also a bit against the philosophy of IOC containers
Thinker
Thinker2y ago
huh well thanks
Thinker
Thinker2y ago
Instantiate a type with constructor arguments provided directly and/or from an IServiceProvider.
That's literally the definition of what I was looking for. Thanks.
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Is there any benefit to write a Property with a private variable ?Why: ```cs private int myVar; public int MyVar { get => myVar; set => myVar = value; } ``` may be beHow add analyzer for suggesting to add missing parameters or removing them?Soo I have analyzer which checks if parameter type is proper soo then I need to make codefix which j[ASP.NET] B-Logic Services, ValidationI am using anemic models, therefore I want to implement Business Logic Services. And the questions Optimizing some string manipulationI want to both substring an input string at the last occurrence of `'/'` and normalize it into *onlyRight way to use Interfaces in Large Scale Blazor Server Web ApplicationsHello there, iam currently working on a <see title> app for something iam not allowed to leak now iaUsing dependency injection for internal classes inside a libraryI'm making a library, where I feel like it would be useful to be able to use Microsoft's dependency System.InvalidOperationException The operation is not allowed on non-connected socketsi have client and server, client creates and connects tcp client to server tcp listener, client tcp HttpClient only working in Main but not in another Class? [Answered]I'm currently following a c# json tutorial and trying to put this code into it's own class so I can Which workload for blazor desktop?In VS2022, which workload(s) do I need to install to create a blazor desktop app?Style dependent on a propertyA Button `ConfirmationButton` should have the `Style= MahApps.Styles.Button.Dialogs.Accent` when the