有没有办法用更精确的东西替换此 Visual Basic 词典中的 'Object' 值类型?

Is there a way I can replace the 'Object' value type in this Visual Basic dictionary with something more precise?

我正在使用一个工厂来 select 来自多个实现相同通用接口的服务。我的工厂是这样写的:

Public Class JurisdictionServiceFactory
    Private JurisdictionTypeMapper As Dictionary(Of String, Object)

    Sub New()
        JurisdictionTypeMapper = New Dictionary(Of String, Object)
        JurisdictionTypeMapper.Add("CountryJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of CountryJurisdiction)))
        JurisdictionTypeMapper.Add("StateJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of StateJurisdiction)))
        JurisdictionTypeMapper.Add("CountyJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of CountyJurisdiction)))
        JurisdictionTypeMapper.Add("CityJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of CityJurisdiction)))
        JurisdictionTypeMapper.Add("OtherJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of OtherJurisdiction)))
    End Sub

    Public Function getJurisdictionService(type As String) As Object
        Return JurisdictionTypeMapper(type)
    End Function
End Class

我想将 'Object' 替换为允许编译器知道对象上存在哪些方法的值类型。例如,我希望能够使用自动完成功能。我试过这样做:Private JurisdictionTypeMapper As Dictionary(Of String, IJurisdictionService(Of )),但我只收到一条消息:"Type expected".

这是 IJurisdictionService:

Public Interface IJurisdictionService(Of t)
    Inherits IDisposable

    Function GetJurisdictionsByCompany(companyId As Integer) As List(Of t)

    Sub AddJurisdiction(jurisdiction As t)
End Interface

这是一个示例实现:

Public Class CountryJurisdictionService
    Implements IJurisdictionService(Of CountryJurisdiction)

    Private jurisdictionRepository As IRepository(Of CountryJurisdiction)

    Public Sub New(jurisdictionRepository As IRepository(Of CountryJurisdiction))
        Me.jurisdictionRepository = jurisdictionRepository
    End Sub

    Public Sub AddJurisdiction(jurisdiction As CountryJurisdiction) Implements IJurisdictionService(Of CountryJurisdiction).AddJurisdiction
        jurisdictionRepository.Add(jurisdiction)
        jurisdictionRepository.Commit()
    End Sub

    Public Function GetJurisdictionsByCompany(companyId As Integer) As List(Of CountryJurisdiction) Implements IJurisdictionService(Of CountryJurisdiction).GetJurisdictionsByCompany
        Return jurisdictionRepository.GetMany(Function(j) j.CompanyID = companyId, False)
    End Function
End Class

编辑: 这是工厂将用于的上下文:

Public Sub AddJurisdiction(jurisdiction)
        Using jurisdictionService = jurisdictionServiceFactory.getJurisdictionService(TypeName(jurisdiction))
            jurisdictionService.AddJurisdiction(jurisdiction)
        End Using
End Sub

谁在呼叫 getJurisdictionService?只要调用者知道他们请求的是什么类型,你就可以做这样的事情。

如何改变这个:

Public Function getJurisdictionService(type As String) As Object
    Return JurisdictionTypeMapper(type)
End Function

为此:

Public Function getJurisdictionService(Of T)() As IJurisdictionService(Of T)
    Return DirectCast(JurisdictionTypeMapper(GetType(T)), IJurisdictionService(Of T))
End Function

调用者这样做是为了获得强类型服务:

service = jurisdictionServiceFactory.getJurisdictionService(Of CountryJurisdictionService)()

当然,字典需要根据类型而不是 class 名称进行键控:

Private JurisdictionTypeMapper As Dictionary(Of Type, Object)

编辑:

鉴于您提供的新细节,在那种情况下我经常喜欢创建基础 class 或父接口以在通用 classes 之间创建兄弟关系,即使这只是一些基于代码的文档:

Public Interface IJurisdictionService
    Inherits IDisposable

End Interface

Public Interface IJurisdictionService(Of t) 
    Inherits IJurisdictionService

    Function GetJurisdictionsByCompany(companyId As Integer) As List(Of t)

    Sub AddJurisdiction(jurisdiction As t)

End Interface

这也适用于 Jurisdiction 对象,它们可以使用父接口或基 class:

Public Interface IJurisdictionService
End Interface

然后Private JurisdictionTypeMapper As Dictionary(Of String, Object)变成Private JurisdictionTypeMapper As Dictionary(Of String, IJurisdictionService)