【问题标题】:How to store VB.Net Generics in a .Net Generic如何将 VB.Net 泛型存储在 .Net 泛型中
【发布时间】:2015-05-14 18:02:21
【问题描述】:

我有一个这样的 VB.Net Generic

Public Class typeableDictionary(of T as baseClass)
    Inherits System.Collections.Generic.Dictionary(of String, T)
    ...
End Class

具有(例如)以下用于存储其中的类:

Public Class derivedClass1
    Inherits baseClass
    ...
End Class

Public Class derivedClass2
    Inherits baseClass
    ...
End Class

这是在我的代码中的不同位置实例化的 - 例如:

Public typedDictionary1(of derivedClass1)
Public typedDictionary2(of derivedClass2)

我的问题很简单:我如何将这些(每个成员的 T 不同)存储在 same 字典中,即

我试过了

Private dictionaryStore as New Dictionary(Of String, typeableDictionary)

但显然这无法编译是它本身不理解typeableDictionary 并期望typeableDictionary(Of <Type>)

我也试过笨重的

Private dictionaryStore as New Dictionary(Of String, Dictionary(Of String, baseClass))

这也失败了。

这里的具体需要是将派生自同一个基类(泛型)的多个类型的字典存储在单个字典中,稍后将从中检索它们并以特定于类型的方式进行处理。我当然可以为每种类型声明一个单独的字典,但这缺乏优雅,我确信 VB.Net 的强大功能可以让我更轻松地解决这个问题。

我在这里遗漏了什么明显的东西吗?到目前为止,它让我的头很痛!事实上,我希望通过输入我的问题,我会更好地理解它,也许 StackOverflow 会识别出一个类似的问题,但到目前为止我不确定它是否有。

我需要进一步的抽象层吗??

【问题讨论】:

  • 这里是否有非泛型基类帮助。 IE。 TypeableDictionary :TypeableDictionary。然后你使用 Dictionary.
  • 认为具有不同 T 的泛型类是相关的,这是一个很常见的错误。它们唯一的共同基类是 System.Object。你在这里有点 一点,你偶然得到了非泛型 IDictionary。是的,它不是类型安全的。不可能。
  • @HansPassant - 那么说编译器不会“查看”派生类来确定其基类是否正确?如果是这样的话,似乎这将是 .Net/VB.Net 团队扩展的一个非常有用的领域。另外汉斯我想“向上箭头”你的回复,但是(因为它在评论中)我不能......
  • 说编译器强制类型安全是正确的。这就是工作。您对 VB.NET 团队没有什么期望,这完全是设计使然。您可以通过谷歌搜索“.net generics covariance”了解更多信息。

标签: .net vb.net generics inheritance collections


【解决方案1】:

您的derivedClass1derivedClass2 应该继承自基类或实现公共接口。然后你定义你的字典为 typeableDictionary(Of baseClass)typeableDictionary(Of yourInterface)

你需要改变

Public typedDictionary1(of derivedClass1)
Public typedDictionary2(of derivedClass2)

Public typedDictionary1 As typeableDictionary(of baseClass)
Public typedDictionary2 As typeableDictionary(of baseClass)

【讨论】:

  • 这就是(我认为)我所做的,但它无法编译:错误 60 类型 'ReferenceData.referenceDataNameDictionary(Of T)' 的值无法转换为 'System.Collections.Generic.Dictionary (字符串,referenceDataBase)'。 C:\...数据\ReferenceData\referenceDataService.vb 112 62
【解决方案2】:

我相信您需要使用接口而不是类来声明您的 typedDictionary。这是你的想法吗?

Public Interface IBaseClass
    Sub GetResult()
End Interface

Public Class typeableDictionary(Of T As IBaseClass)
    Inherits System.Collections.Generic.Dictionary(Of String, T)
    REM some code here
End Class

Public Class DerivedClass1
    Implements IBaseClass

    Public Sub GetResult() Implements IBaseClass.GetResult
        Console.WriteLine("This is DerivedClass1")
    End Sub
End Class

Public Class DerivedClass2
    Implements IBaseClass

    Public Sub GetResult() Implements IBaseClass.GetResult
        Console.WriteLine("This is DerivedClass2")
    End Sub
End Class

    Sub Main()
        Dim myTypedDictionary As New typeableDictionary(Of IBaseClass)
        myTypedDictionary.Add("1", New DerivedClass1)
        myTypedDictionary.Add("2", New DerivedClass2)
        Console.WriteLine("this is a vb app")
        Console.ReadKey()
    End Sub

【讨论】:

    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多