Base class methods not accessible from outside parent class? [duplicate]
Base class methods not accessible from outside parent class? [duplicate]
This question already has an answer here:
I wanted to code a dependency provider to register my dependencies. I inherit the ServiceCollection class, meaning I should get all the methods in that class, right? Wrong, so it seems.
Ad you can see, I inherit it, register it using the this. keyword, but without that it errors.
this.
public class DependencyProvider : ServiceCollection, IDependencyProvider
public DependencyProvider() : base()
Register();
public void Register()
this.AddSingleton<ICoreContext, CoreContext>();
Calling AddSingleton without this. throws an error saying it can't find the method, while adding this. it works as expected. What if I need access it on an instance of the class?
AddSingleton
this.
this
It also means that if I want to access any of the methods outside of this class, I have to duplicate the method inside the DependencyProvider class, which just isn't very efficient.
Here is the IDependencyProvider interface
IDependencyProvider
public interface IDependencyProvider : IServiceCollection
void Register();
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
AddSingleton
ServiceCollection
That's because
AddSingleton is an extension method. It's not defined in ServiceCollection and you have to pass it a ServiceCollection for it to work. Basically that translates to ServiceCollectionServiceExtensions.AddSingleton<ICoreContext, CoreContext>(this);– juharr
Aug 31 at 15:01
AddSingleton
ServiceCollection
ServiceCollection
ServiceCollectionServiceExtensions.AddSingleton<ICoreContext, CoreContext>(this);
Is there any way to access the methods directly from the base class of
DependencyProvider, just like I was trying to?– fskdjwe
Aug 31 at 15:04
DependencyProvider
I thought I had already answered you this here...?
– Camilo Terevinto
Aug 31 at 15:05
No, you just use the extension method. The extension method will work with classes inherited from
ServiceCollection.– Amy
Aug 31 at 15:10
ServiceCollection
1 Answer
1
Like I answered you here, AddSingleton is an extension method on IServiceCollection. That means that if you want to use it inside DependencyProvider you need to either use this.AddSingleton or refer to the method directly ServiceCollectionServiceExtensions.AddSingleton.
AddSingleton
IServiceCollection
DependencyProvider
this.AddSingleton
ServiceCollectionServiceExtensions.AddSingleton
Calling AddSingleton without this. throws an error saying it can't find the method, while adding this. it works as expected. What if I need access it on an instance of the class?
Well, in that case you just use it as you'd expect:
var myProvider = new DependencyProvider();
myProvider.AddSingleton<ICoreContext, CoreContext>();
AddSingletonmust be an extension method and not a method fromServiceCollection.– Amy
Aug 31 at 15:01