接口上的扩展方法未显示

问题描述 投票:2回答:1

我正在研究一个类库,将一些导航功能扩展到开源框架。我不希望将这些功能作为拉取请求直接添加到框架中,因为它不会被所有人使用,也不希望不必要地膨胀框架的大小。所以我的想法是实现作为扩展方法。

这就是代码在框架中的实现方式。

public interface INavigationService
{
    Task Method1();
    Task Method2();
}

public class NavigationService : INavigationService
{
    public async Task Method1
    {
      //Implementation
    }

    public async Task Method1
    {
      //Implementation
    }
}

在框架内的基本视图模型中,存在类型为INavigationService的属性。

public class BaseFrameworkViewModel
{
   public INavigationService NavigationService {get; set;}

   //Other implementations.
} 

使用框架的开发人员继承应用程序项目中的BaseFrameworkViewModel并使用方法Method1和Method2。

public class BaseViewModel : BaseFrameworkViewModel
{
     NavigationService.Method1();
     NavigationService.Method2();
}

现在,我想将Method3添加到NavigationService类。为此,我创建了一个.NetStandard类库并添加了一个静态类,并创建了一个类似于下面的扩展方法。但我没有看到BaseViewModel中的方法。

我的实施

public static class NavigationExtensionService
{
   public static async Task Method3(this INavigationService navigationService, string a1)
   {
       //Method Implementation
   } 
} 

我已经构建了我已经创建并在应用程序项目中引用的库,但是当我尝试下面的代码时,我没有在NavigationService上获得Method3。

NavigationService.Method3("abcd") //does not resolve Method3

感谢有人可以帮助解决这个问题。

c# .net extension-methods
1个回答
4
投票

除了引用程序集之外,还需要在要使用扩展方法的文件中导入名称空间。

© www.soinside.com 2019 - 2024. All rights reserved.