比较通用字段

发布于 2024-07-10 08:59:48 字数 1605 浏览 7 评论 0原文

我有一些泛型类型,如下所示:

public struct Tuple<T1, T2> { ... }
public struct Tuple<T1, T2, T3> { ... }
etc.

理论上,这些类型应该能够将自己与相同类型的其他值进行比较,以便我可以编写以下类型的代码:

List<Tuple<Type, String>> l = new List<Tuple<Type, String>>();
l.Add(new Tuple<Type, String>(typeof(ISomeInterface), "123"));
if (l.Contains(new Tuple<Type, String>(typeof(ISomeOtherInterface), "123"))
    ...

不幸的是,我的代码中有一个错误,并且那么问题就变成了如何正确地做到这一点。

该错误与我的 CompareTo> 实现有关,它基本上如下所示:

Int32 result = HelperMethods.CompareTwoFields<T1>(_Value1, other._Value1);
if (result != 0)
    return result;

Int32 result = HelperMethods.CompareTwoFields<T2>(_Value2, other._Value2);
if (result != 0)
    return result;

return 0;

HelperMethods.CompareTwoFields 如下所示:

internal static Int32 CompareTwoFields<T>(T field1, T field2)
{
    Int32 result = 0;
    if (ReferenceEquals(field1, null) != ReferenceEquals(field2, null))
        result = ReferenceEquals(field1, null) ? -1 : +1;
    else if (!ReferenceEquals(field1, null) && field1 is IComparable<T>)
        result = ((IComparable<T>)field1).CompareTo(field2);
    else if (!typeof(T).IsValueType)
    {
        if (Object.ReferenceEquals(field1, field2))
            return 0;
        else
            return field1.ToString().CompareTo(field2.ToString());
    }
    return result;
}

最后一个 if 语句现在有一些内容我用来修复该错误,但这是否正确?

基本上,如何比较两个 Type 对象? 除了将它们转换为字符串并进行比较之外,是否有有意义的比较?

I have some generic types, like the following:

public struct Tuple<T1, T2> { ... }
public struct Tuple<T1, T2, T3> { ... }
etc.

These should in theory be able to compare themselves against other values of the same type, so that I can write the following type of code:

List<Tuple<Type, String>> l = new List<Tuple<Type, String>>();
l.Add(new Tuple<Type, String>(typeof(ISomeInterface), "123"));
if (l.Contains(new Tuple<Type, String>(typeof(ISomeOtherInterface), "123"))
    ...

unfortunately, there was a bug in my code, and the question then becomes how to do this correctly.

The bug had to do with my implementation of CompareTo>, which basically looks like this:

Int32 result = HelperMethods.CompareTwoFields<T1>(_Value1, other._Value1);
if (result != 0)
    return result;

Int32 result = HelperMethods.CompareTwoFields<T2>(_Value2, other._Value2);
if (result != 0)
    return result;

return 0;

HelperMethods.CompareTwoFields looks like this:

internal static Int32 CompareTwoFields<T>(T field1, T field2)
{
    Int32 result = 0;
    if (ReferenceEquals(field1, null) != ReferenceEquals(field2, null))
        result = ReferenceEquals(field1, null) ? -1 : +1;
    else if (!ReferenceEquals(field1, null) && field1 is IComparable<T>)
        result = ((IComparable<T>)field1).CompareTo(field2);
    else if (!typeof(T).IsValueType)
    {
        if (Object.ReferenceEquals(field1, field2))
            return 0;
        else
            return field1.ToString().CompareTo(field2.ToString());
    }
    return result;
}

The last if-statement there is something I tucked on now to fix the bug, but is this correct?

Basically, how do I compare two Type objects? Is there a meaningful comparison for these, except just to convert them to a string and compare?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

怎樣才叫好 2024-07-17 08:59:48

您的意思是:

bool equal = EqualityComparer<T>.Default.Equals(val1,val2);

这适用于 IEquatable (对于 T : IEquatable),否则回退到 object.Equals

还有用于不等式比较(大于/小于)的 Comparer.Default

int delta = Comparer<T>.Default.Compare(val1,val2);

这使用 T : IComparableT : IComparable代码> 否则。

顺便说一句,Type 应该只使用 object.Equals 提供的常规引用比较,因此它应该可以与上面的内容一起正常工作。

Do you mean:

bool equal = EqualityComparer<T>.Default.Equals(val1,val2);

This works with IEquatable<T> (for T : IEquatable<T>), else falls back to object.Equals.

There is also Comparer<T>.Default for inequality comparisons (greater/less):

int delta = Comparer<T>.Default.Compare(val1,val2);

This uses T : IComparable<T>, or T : IComparable otherwise.

By the way, Type should just use the regular reference compare provided by object.Equals, so it should just work fine with the above.

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