在Visual Studio中隐藏Intellisense中的接口方法

Nee*_*ima 3 .net intellisense interface visual-studio

我有一个界面,它有很多方法.在实现类时,我不应该在Intellisense中看到这些方法.我怎样才能做到这一点?

par*_*mar 6

您可以在方法上使用EditorBrowsableAttribute.

EditorBrowsableAttribute指定在编辑器中可以查看属性或方法.EditorBrowsableAttribute是设计器的提示,指示是否要显示属性或方法.您可以在可视化设计器或文本编辑器中使用此类型来确定用户可见的内容.例如,Visual Studio中的IntelliSense引擎使用此属性来确定是否显示属性或方法.

就像是

 

   [EditorBrowsable(EditorBrowsableState.Never)]
   public void GetId()
   {
   }
Run Code Online (Sandbox Code Playgroud)


Run*_* FS 5

您需要明确地实现它们.

如果您的界面是:

interface IFoo{
  void DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

然后你可以在如下的类中明确地实现它:

class Foo : IFoo {
  void IFoo.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)

话虽这么说,你可能想知道为什么要这样做.接口通常用于定义给定类型的对象能够具有的契约.想要隐藏它可能是一种在设计过程中误入歧途的气味.当然,还有许多使用显式实现的有效案例.例如,Dictionary对IEnumerable中的一些方法有明确的实现.但隐藏应该是比规则更多的例外.

如果声明方法将显示的接口类型的变量/成员,如果声明它们将不显示的具体类型的成员/变量.由于您通常应该优先使用具体类的接口,因此这又暗示您可能想要查看想要隐藏接口声明的方法的原因