构建 API 结构,模块成员的范围限定在错误的命名空间
Building an API structure, members of a module are scoped to wrong namespace
我正在使用 Visual Studio 2013,试图开发一个自己的 API 具有此成员结构:
问题是 Extensions
模块的成员在 Elektro.Xml.Extensions
和 Elektro.Xml
中公开:
为什么会这样?以及如何修复它?
Extensions.vb
Public Module Extensions
#Region " Public Extension Methods "
#Region " Type Conversion "
<Extension>
Public Function ToXDocument(ByVal sender As XmlDocument) As XDocument
...
End Function
#End Region
#End Region
End Module
XmlUtil.vb
Public NotInheritable Class XmlUtil
#Region " Public Methods "
Public Shared Function XmlBeautifier(...) As String
...
End Function
#End Region
End Class
这是标准,而不是 "fixed"。有关更多详细信息,请参阅 Visual Basic Language Specification:
的第 7.7 节
A member of a standard module has two fully qualified names, one without the standard module name and one with the standard module name. More than one standard module in a namespace may define a member with a particular name; unqualified references to the name outside of either module are ambiguous. For example:
Namespace N1
Module M1
Sub S1()
End Sub
Sub S2()
End Sub
End Module
Module M2
Sub S2()
End Sub
End Module
Module M3
Sub Main()
S1() ' Valid: Calls N1.M1.S1.
N1.S1() ' Valid: Calls N1.M1.S1.
S2() ' Not valid: ambiguous.
N1.S2() ' Not valid: ambiguous.
N1.M2.S2() ' Valid: Calls N1.M2.S2.
End Sub
End Module
End Namespace
检查 github 中的随机第 3 方 API(我通常这样做是为了发现新技术和获取知识)我发现要解决这个问题,开发人员只需设置 HideModuleName
属性到模块和 voilá.
我正在使用 Visual Studio 2013,试图开发一个自己的 API 具有此成员结构:
问题是 Extensions
模块的成员在 Elektro.Xml.Extensions
和 Elektro.Xml
中公开:
为什么会这样?以及如何修复它?
Extensions.vb
Public Module Extensions
#Region " Public Extension Methods "
#Region " Type Conversion "
<Extension>
Public Function ToXDocument(ByVal sender As XmlDocument) As XDocument
...
End Function
#End Region
#End Region
End Module
XmlUtil.vb
Public NotInheritable Class XmlUtil
#Region " Public Methods "
Public Shared Function XmlBeautifier(...) As String
...
End Function
#End Region
End Class
这是标准,而不是 "fixed"。有关更多详细信息,请参阅 Visual Basic Language Specification:
的第 7.7 节A member of a standard module has two fully qualified names, one without the standard module name and one with the standard module name. More than one standard module in a namespace may define a member with a particular name; unqualified references to the name outside of either module are ambiguous. For example:
Namespace N1
Module M1
Sub S1()
End Sub
Sub S2()
End Sub
End Module
Module M2
Sub S2()
End Sub
End Module
Module M3
Sub Main()
S1() ' Valid: Calls N1.M1.S1.
N1.S1() ' Valid: Calls N1.M1.S1.
S2() ' Not valid: ambiguous.
N1.S2() ' Not valid: ambiguous.
N1.M2.S2() ' Valid: Calls N1.M2.S2.
End Sub
End Module
End Namespace
检查 github 中的随机第 3 方 API(我通常这样做是为了发现新技术和获取知识)我发现要解决这个问题,开发人员只需设置 HideModuleName
属性到模块和 voilá.