双向字典不区分大小写?
问题:我使用在这里找到的双向字典类: 这个
问题是,我需要 - 不区分大小写(StringComparer.OrdinalIgnoreCase)
我想扩展它以覆盖 IEqualityComparer 构造函数。 我已将其转换为 VB(就像魅力一样),但我在实现比较器“传输”时遇到了麻烦。
问题是,在我的参数中:
ByVal x As System.Collections.Generic.IEqualityComparer(Of TKey)
但是字典 secondaryToFirst 的类型为 TValue, TKey,这会杀死我的 IEqualityComparer,它需要为 TValue 而不是 TKey 类型...
我如何对这个比较器进行类型转换?
如果某个地方有另一个 BiDictionaryOneToOne 类,并且不区分大小写,那也没关系(只要该库的大小/内存消耗和 .NET 2.0 不是巨大的)
Public Class BiDictionaryOneToOne(Of TKey, TValue)
Public Sub New(ByVal x As System.Collections.Generic.IEqualityComparer(Of TKey))
Dim y As System.Collections.Generic.IEqualityComparer(Of TValue) = StringComparer.OrdinalIgnoreCase
firstToSecond = New Dictionary(Of TKey, TValue)(x)
secondToFirst = New Dictionary(Of TValue, TKey)(y)
End Sub
编辑:
好吧,当然这只有在 TKey & 的情况下才有可能。正如 John 指出的那样,TValue 是字符串类型,但如果它们相同,仍然可以使用 try/catch ,如下所示:
Public Sub New(ByVal cmpFirstDirection As System.Collections.Generic.IEqualityComparer(Of TKey))
Try
Dim cmpOppositeDirection As System.Collections.Generic.IEqualityComparer(Of TValue) = CType(cmpFirstDirection, System.Collections.Generic.IEqualityComparer(Of TValue))
firstToSecond = New Dictionary(Of TKey, TValue)(cmpFirstDirection)
secondToFirst = New Dictionary(Of TValue, TKey)(cmpOppositeDirection)
Catch ex As Exception
firstToSecond = New Dictionary(Of TKey, TValue)(cmpFirstDirection)
secondToFirst = New Dictionary(Of TValue, TKey)
End Try
End Sub
Public Sub New(ByVal cmpFirstDirection As System.Collections.Generic.IEqualityComparer(Of TKey), ByVal cmpOppositeDirection As System.Collections.Generic.IEqualityComparer(Of TValue))
firstToSecond = New Dictionary(Of TKey, TValue)(cmpFirstDirection)
secondToFirst = New Dictionary(Of TValue, TKey)(cmpOppositeDirection)
End Sub
Question: I use the bidirectional dicionary class I found here:
Bidirectional 1 to 1 Dictionary in C#
The problem is, I need this - case insensitive (StringComparer.OrdinalIgnoreCase)
I wanna extend it to cover the IEqualityComparer constructor.
I've converted it to VB (works like a charm), but I have trouble implementing the comparer 'transfer'.
The problem is, in the parameters I have:
ByVal x As System.Collections.Generic.IEqualityComparer(Of TKey)
But the dicionary secondToFirst is of type TValue, TKey, which kills my IEqualityComparer, which needs to be of type TValue instead of TKey...
How do I typecast this comparer ?
If somewhere there's another class for BiDictionaryOneToOne, with case-insensitiveness, that's also OK (as long as that library isn't monumental in size/memory consumption and .NET 2.0)
Public Class BiDictionaryOneToOne(Of TKey, TValue)
Public Sub New(ByVal x As System.Collections.Generic.IEqualityComparer(Of TKey))
Dim y As System.Collections.Generic.IEqualityComparer(Of TValue) = StringComparer.OrdinalIgnoreCase
firstToSecond = New Dictionary(Of TKey, TValue)(x)
secondToFirst = New Dictionary(Of TValue, TKey)(y)
End Sub
Edit:
OK, of course it's only possible if TKey & TValue are of type string, as John points out, but in case they are the same, it's still possible with try/catch like this:
Public Sub New(ByVal cmpFirstDirection As System.Collections.Generic.IEqualityComparer(Of TKey))
Try
Dim cmpOppositeDirection As System.Collections.Generic.IEqualityComparer(Of TValue) = CType(cmpFirstDirection, System.Collections.Generic.IEqualityComparer(Of TValue))
firstToSecond = New Dictionary(Of TKey, TValue)(cmpFirstDirection)
secondToFirst = New Dictionary(Of TValue, TKey)(cmpOppositeDirection)
Catch ex As Exception
firstToSecond = New Dictionary(Of TKey, TValue)(cmpFirstDirection)
secondToFirst = New Dictionary(Of TValue, TKey)
End Try
End Sub
Public Sub New(ByVal cmpFirstDirection As System.Collections.Generic.IEqualityComparer(Of TKey), ByVal cmpOppositeDirection As System.Collections.Generic.IEqualityComparer(Of TValue))
firstToSecond = New Dictionary(Of TKey, TValue)(cmpFirstDirection)
secondToFirst = New Dictionary(Of TValue, TKey)(cmpOppositeDirection)
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试编写一个通用双字典,它可以具有任何类型的键/值对组合。使用 StringComparer 比较两个整数是什么意思?我建议您更改构造函数以采用两个
IEqualityComparer
,一个用于TKey
,一个用于TValue
。然后,您可以通过传入两个适当的IEqualityComparer(Of String)
值来创建不区分大小写的BiDictionaryOneToOne(Of String, String)
。You're trying to write a generic bi-dictionary which could have any key/value pair combination of types. What does it mean to compare two integers using
StringComparer
? I suggest you change your constructor to take twoIEqualityComparer
s, one forTKey
and one forTValue
. You can then create aBiDictionaryOneToOne(Of String, String)
which is case-insensitive by passing in two appropriateIEqualityComparer(Of String)
values.