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 must be an extension method and not a method from ServiceCollection.
– Amy
Aug 31 at 15:01


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>();

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Node.js puppeteer - Use values from array in a loop to cycle through pages

How do I collapse sections of code in Visual Studio Code for Windows?