Reputation: 7392
I am working on a class library to extend some navigation functionalities to an open-source framework. I prefer not to add these functionalities as a pull request directly to the framework because it wont be used by everyone and do not want to unnecessarily bloat the size of the framework. So my idea is to implement as an extension method.
This is how the code is implemented in the framework.
public interface INavigationService
{
Task Method1();
Task Method2();
}
public class NavigationService : INavigationService
{
public async Task Method1
{
//Implementation
}
public async Task Method1
{
//Implementation
}
}
And in the base viewmodel within the framework there is property of type INavigationService.
public class BaseFrameworkViewModel
{
public INavigationService NavigationService {get; set;}
//Other implementations.
}
And the developers who consume the framework do inherit from the BaseFrameworkViewModel in the app project and use methods Method1 and Method2.
public class BaseViewModel : BaseFrameworkViewModel
{
NavigationService.Method1();
NavigationService.Method2();
}
Now, I would like to add Method3 to NavigationService class. So for that I created a .NetStandard class library and added a static class and created an extension method like below. But I don't get to see the method in the BaseViewModel.
My implementation
public static class NavigationExtensionService
{
public static async Task Method3(this INavigationService navigationService, string a1)
{
//Method Implementation
}
}
I have build the library that I have created and have referenced in the application project, however when i try the below , i don't get the Method3 on NavigationService.
NavigationService.Method3("abcd") //does not resolve Method3
Appreciate if someone can help to resolve this.
Upvotes: 2
Views: 298
Reputation: 22501
In addition to referencing the assembly, you also need to import the namespace in the files where you want to use the extension method.
Upvotes: 4