VB6中的Attribute关键字有什么作用?

What does the Attribute keyword do in VB6?

我正在将一些 VB6 代码转换为 VB.Net。由于我的 VB6 安装似乎已损坏无法修复我正在使用记事本阅读原始源代码并可以在文件顶部附近看到:-

Attribute VB_Name = "clsBulge"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Description = "Some text here"
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Member0" ,"collBulges"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"

及以下作品:-

Public Property Let Amplitude(ByVal vData As Double)
Attribute Amplitude.VB_Description = "Some text here"
    mvaInternal = vData
End Property

问题是,在转换为 VB.Net 时我是否需要为此担心?如果是这样,我在哪里可以找到所有这些东西的含义?

给出的问题和答案 here suggests not, but isn't really an authoritative source. A similar question asked here 很快就变得无关紧要了。

我写了一些关于 VB Attributes in a VBA context 的内容,但我可以在这里总结一下这些内容。


Attribute VB_Name = "clsBulge"

不言自明,这是 class 的名称。要创建它的新实例,您必须调用 Dim foo = New clsBulge


Attribute VB_GlobalNameSpace = False

这个有点有趣,通过将它设置为 true,a global default instance will be created. When the application starts up, an instance of the class will be automatically created and accessible via simple name access 它的 public 成员。这有点难以解释,但是如果您查看对象资源管理器中的内置 VBA 函数,您将很快看到它如何允许您 "shortcut" 和 "namespace"。

除非将其设置为 True,否则在移植时您不必担心这一点。任何设置为 True 的 classes 都会让你头疼,因为这个 "static" class 的客户不必通过它的显式名称来调用它,但必须在你之后已将您的代码移植到 .Net。


Attribute VB_PredeclaredId = False

VB_GlobalNameSpace 相关,但语义略有不同。它大致相当于 .Net 中的 Static class。只是...不是,因为您仍然可以创建 class 的其他实例。它也是 described in the link above 为:

A class module has a default instance variable if its VB_PredeclaredId attribute or VB_GlobalNamespace attribute has the value "True". This default instance variable is created with module extent as if declared in a containing an element whose was the name of the class.

If this class module’s VB_PredeclaredId attribute has the value "True", this default instance variable is given the name of the class as its name. It is invalid for this named variable to be the target of a Set assignment. Otherwise, if this class module’s VB_PredeclaredId attribute does not have the value "True", this default instance variable has no publicly expressible name.

If this class module’s VB_GlobalNamespace attribute has the value "True", the class module is considered a global class module, allowing simple name access to its default instance’s members...

Note that if the VB_PredeclaredId and VB_GlobalNamespace attributes both have the value "True", the same default instance variable is shared by the semantics of both attributes.


Attribute VB_Creatable = True

这个也很有趣。它与范围规则有关。本质上,如果将其设置为 True,则可以从任何地方调用它的构造函数。它是 Public,可以从任何地方创建。但是如果设置为False,就相当于有了一个Internal ctor。


Attribute VB_Exposed = False

只是控制模块的范围。 True 是 Public,False 是内部。它与 VB_Creatablecreate a matrix of scoping behavior.

结合使用
Attribute VB_Description = "Some text here"

大致相当于 <Summary> 文档注释。此文本将显示在 VB6(和 VBA)对象浏览器中。如果我没记错的话,许多其他支持 COM 的语言出于相同目的使用它。您实际上可以通过使用 ComponentModel.Description attribute. 为您的 COM 公开的 .Net 库生成这种确切的行为。


Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Member0" ,"collBulges"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"

这些是 IDE Add-Ins 使用的自定义属性。我不能具体说明它们做了什么,但它们不太可能需要保留。