【问题标题】:Can the XML Documentation added in a Com Visible dll be viewed in the VBA editor or Object Browser?可以在 VBA 编辑器或对象浏览器中查看添加到 Com Visible dll 中的 XML 文档吗?
【发布时间】:2017-06-03 20:01:08
【问题描述】:

我有一个 COM 可见的 dll,可以在 VBA 中正常工作。我希望能够在 VBA 编辑器和 VBA 对象浏览器中看到 XML 文档,这可能吗?

XML 文档是指摘要、参数和备注。

''' <summary>
''' Method to add some string.
''' </summary>
''' <param name="aString">The text to add.</param>
''' <remarks>Some remarks go here.</remarks>
Public Sub SomeSub(ByVal aString As String)


End Sub

如果无法为想要在 VBA 中使用我的 dll 的用户提供有关可用的类、方法和函数的信息,我有哪些选择? Visual Studio 在这里提供了什么? (因此我添加了 Visual Studio 作为标签)

【问题讨论】:

  • 没有。您必须使用 [Description] 属性。
  • 在 VB.NET 中这意味着像这样装饰方法? _ 或者还有更多内容吗?
  • @Hans 看来我可以将 _ 添加到类、方法、枚举、事件中,但我是否正确地说它不适用于公共属性?
  • 谢谢 Hans,但我认为在 VB.NET 中您不能在界面中使用 Getter 和 Setter?

标签: vb.net vba visual-studio


【解决方案1】:

正如汉斯·帕桑特所说:“不。您必须使用 [Description] 属性'。只是为了扩展和包含来自 SO 问题 Hans 然后引用的信息,似乎 Description 属性必须是有关方法参数的所有文档的载体,因为 COM 接口定义语言 IDL 不支持这种粒度。

别担心,似乎可以在描述字段中输入大量文本。

更新:一些 VB.NET 示例代码的附加信息。

  1. 以管理员权限打开 Visual Studio(需要管理员才能注册)
  2. 创建 VB.NET 类库
  3. 转到项目属性
  4. 在构建选项卡上选中 Register for Interop 复选框
  5. 将默认类的代码更改为以下

    
    Imports System.ComponentModel
    
    

    Public Interface Interface1

    <Description("Interface Description - Here we can put method information as well as parameter information")>
    Function AddNumbers(ByVal X As Integer, ByVal Y As Integer)
    
    <Description("Interface Description - Attempt to put description on Name")>
    Property Name() As String
    

    End Interface

    '@987654322@

    Public Class ComClass1 Implements Interface1 Dim sName As String

    ' These  GUIDs provide the COM identity for this class
    ' and its COM interfaces. You can generate
    ' these guids using guidgen.exe
    Public Const ClassId As String = "7666AC25-855F-4534-BC55-27BF09D49D46"
    Public Const InterfaceId As String = "54388137-8A76-491e-AA3A-853E23AC1217"
    Public Const EventsId As String = "EA329A13-16A0-478d-B41F-47583A761FF2"
    
    Public Sub New()
        MyBase.New()
    End Sub
    
    <Description("Here we can put method information as well as parameter information")>
    Function AddNumbers(ByVal X As Integer, ByVal Y As Integer) Implements Interface1.AddNumbers
        AddNumbers = X + Y
    End Function
    
    Property Name() As String Implements Interface1.Name
        <Description("Attempt to put description on Name")>
        Get
            Return sName
        End Get
        <Description("Attempt to put description on Name")>
        Set(ByVal Value As String)
            sName = Value
        End Set
    End Function
    

    End Class

还要添加一个新的项目项,一个界面,并粘贴到这段代码中

  1. 构建成功后打开Excel,创建工作簿

  2. 在 VBA 中转到 Tools->References ,导航到 ClassLibrary 并检查

  3. 转到对象浏览器并找到您的 ClassLibrary 和 ComClass1。

  4. 对于 AddNumbers 方法,您现在应该看到描述

  1. 还可以使用 Hans Passant 关于在属性的 getter 和 setter 上使用 Description 属性的提示进行验证。

但你是对的@DARBID,对象浏览器显示接口名称中缺少描述属性。

我只能建议您使用 MIDL 编译器来获取程序集的接口定义语言 (IDL),修改此 IDL 以提供 Description 属性,然后重新编译并邀请您的用户浏览您修改后的类型库。这是 IDL 的样子


  [
      odl,
      uuid(54388137-8A76-491E-AA3A-853E23AC1217),
      version(1.0),
      dual,
      oleautomation,
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "ClassLibrary4.ComClass1+_ComClass1")    

    ]
    interface _ComClass1 : IDispatch {
        [id(0x00000001), helpstring("Class Description Here we can put method information as well as parameter information")]
        HRESULT AddNumbers(
                        [in] long X, 
                        [in] long Y, 
                        [out, retval] VARIANT* pRetVal);
        [id(0x00000002), propget, helpstring("Class Description Attempt to put description on Name")]
        HRESULT Name([out, retval] BSTR* pRetVal);
        [id(0x00000002), propput, helpstring("Class Description Attempt to put description on Name")]
        HRESULT Name([in] BSTR pRetVal);
    };

您可以使用 Windows SDK 中的 OLEView.exe 查看计算机上任何 COM 库的 IDL。

或者也许有人可以找到一种将获取和设置放入 VB 接口的方法。我不在 VB.NET 中编程,我使用 VBA 和 C#。抱歉,我现在没有主意了。

【讨论】:

  • VB.NET 中的属性怎么样?
  • VB.NET 属性可行且经过验证。使用示例代码更新了答案。
  • 非常感谢您提供的非常容易理解的示例。我已经完全编写了接口,但我看到你没有使用接口,而是让编译器为你做。在上面我和 Hans 的 cmets 中,您会看到我们正在讨论接口。所以我必须为此删除所有接口?
  • 等等,我喜欢分离接口和类的方法。我从来没有在 VB.NET 中这样做过。你为什么不发布一些代码?或者,您是否将 Description 属性放在 Class 或 Interface 或两者上?
  • 等等,我有个主意。我确认您的问题并将修改我的答案(对我和您一样详细)。我的想法是你得到你 1) 构建你的组件 2) 获取 IDL 3) 修改 IDL 4) 将修改后的 IDL 重新编译为类型库 5) 邀请客户端程序员对你的新类型库使用工具引用。
猜你喜欢
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多