双向字典不区分大小写?

发布于 2024-10-18 23:13:12 字数 1998 浏览 3 评论 0原文

问题:我使用在这里找到的双向字典类: 这个

问题是,我需要 - 不区分大小写(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2024-10-25 23:13:12

您正在尝试编写一个通用双字典,它可以具有任何类型的键/值对组合。使用 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 two IEqualityComparers, one for TKey and one for TValue. You can then create a BiDictionaryOneToOne(Of String, String) which is case-insensitive by passing in two appropriate IEqualityComparer(Of String) values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文