【问题标题】:Interface implementation for VSTO Aplication.Factory class is missing methodsVSTO Aplication.Factory 类的接口实现缺少方法
【发布时间】:2020-03-07 19:55:56
【问题描述】:

我正在阅读 Microsoft Visual Studio Tools for Office (VSTO) 框架生成的 C# 代码,并且遇到了一个我不理解的案例。我将其简化如下(我省略了方法参数):

public interface Tools.Factory
{
    RibbonFactory GetRibbonFactory();   // OK
    AddIn CreateAddIn(...);                                         // Missing
    CustomTaskPaneCollection CreateCustomTaskPaneCollection(...);   // Missing
    SmartTagCollection CreateSmartTagCollection(...);               // Missing
}

public interface ApplicationFactory : Tools.Factory
{
    SmartTag CreateSmartTag(...);       // OK
    Action CreateAction(...);           // OK
    Document GetVstoObject(...);        // OK
    bool HasVstoObject(...);            // OK
}   

public ThisAddIn(ApplicationFactory factory, IServiceProvider serviceProvider) : 
        base(factory, serviceProvider, "AddIn", "ThisAddIn")
{
    Globals.Factory = factory;
}

ThisAddIn 构造函数是 VSTO 自动生成的代码,位于 Designer.cs 文件中。它传递了一个实现ApplicationFactory 接口的参数(factory)。使用F12,我追踪到了如图所示的界面。该接口反过来继承了Tools.Factory 接口,我也列出了。

我不明白的地方:当我在编辑器中为Globals.Factory 变量(如下所示)触发 IntelliSense 时,我只看到两个接口中列出的八个方法中的五个(在代码清单中注释为 OK )。缺少来自Tools.Factory 接口的三个方法的实现(在代码清单中注释为缺少)。这是为什么呢?

注意:代码运行良好。

【问题讨论】:

  • 你能调用方法吗?有一些属性可以让您简单地隐藏 IntelliSense 中的内容,例如 EditorBrowsable
  • @Zer0 你是对的!我可以调用缺少的方法。请张贴作为答案,我会接受。
  • @Zer0 其实缺少的方法都标有[EditorBrowsable(EditorBrowsableState.Never)],我完全忽略了,因为我从来没见过。你真是个该死的天才。 :)

标签: c# visual-studio interface vsto office-addins


【解决方案1】:

EditorBrowsable 等属性可以对设计器和 IntelliSense 隐藏方法和属性。

在这种情况下,提到的方法存在并且可以调用,但被隐藏了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-31
    • 2020-11-13
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多