我正在尝试将私有字典中的KeyCollection暴露给COM,因此它可以支持IEnumerable和ICollection,就像......
<ComVisible(True)> _
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IEnumerableAndCollection
Inherits IEnumerable
Inherits ICollection
End Interface
Public ReadOnly Property Keys() As IEnumerableAndCollection
Get
Return pDictionary.Keys
End Get
End Property
KeyCollections 实现IEnumerable和ICollection,但不是(显然)IEnumerableAndCollection。是否有某种铸造/加宽/等覆盖这种情况?或者我可以将新界面扩展到它上面(尝试但失败了)?
答案 0 :(得分:0)
除了实现两个属性之外,我认为你没有太多选择:
Public ReadOnly Property KeysEnumerable() As IEnumerable
Get
Return pDictionary.Keys
End Get
End Property
Public ReadOnly Property KeysCollection() As ICollection
Get
Return pDictionary.Keys
End Get
End Property
或许在COM端你可以有两个指向同一个对象的变量:
Dim keysC As ICollection = GetObject("myexe.exe", "pDictionary.Keys")
Dim keysE As IEnumerable = GetObject("myexe.exe", "pDictionary.Keys")