Reputation: 3556
I've added the following extension method to my project.
public static class Extensions
{
public static T Get<T>(this IConfiguration self, string path)
{
IConfigurationSection section = self.GetSection(path);
T output = section.Get<T>();
return output;
}
}
For some reason, it seems not to come through in intellisense and the compiler nags that a string can't be passed as a second argument. Googling verified that extension methods do work on interfaces and that the only requirement is that the signature differs, as they can't override.
You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called.
The way I see it, they do differ, because the original has a different pass in parameter than my string version.
public static T Get<T>(this IConfiguration self, string x) { ... }
public static T Get<T>(this IConfiguration self, Action<BinderOptions> x) { ... }
Extending for another class seems to work too as described at MSDN.
What am I missing?
Upvotes: 4
Views: 1872
Reputation: 601
I can confirm that VS2019 cannot recognize the extension method with signature that has already been imported.
You should be able to use the extension method by using a namespace containing the class.
using <yourextensionmethodnamespace>;
And then calling it like this
configuration.Get<WhateverType>("key");
Upvotes: 1