如何混合/修复 IComparable 和 IComparable混乱

发布于 2024-07-25 14:22:50 字数 766 浏览 4 评论 0原文

我有一个辅助函数,它基本上在两个对象上调用 CompareTo,但会进行一些特殊的极端情况检查、转换等。

最初我是这样编写函数的:

public static bool BetterCompare(IComparable lhs, IComparable rhs, out retCode)
{
    ...
    retCode = lhs.CompareTo(rhs);
    ...
}

但问题是,如果我有一个 class AwesomeClass : IComparable< ;AwesomeClass>。 事实上,我有几个,因为一些旧的 IComparable 类已经消失了 IComparable。 然而编译器会生气,因为它无法将这些新对象转换为 IComparable。 我不知道这是否会让情况变得更糟,但其中一些是抽象的(尽管抽象类确实提供了实现)。

我如何表达“我想要两个可以调用 CompareTo 的对象”而不让编译器给我任何提示。 最好,新函数不应看起来像 BetterCompare(this, that, out retCode);,而只是“做正确的事”。 或者是否有更好的方法来做到这一点,而无需触及每个类,使它们同时成为 IComparableIComparable

I have a helper function, which basically calls CompareTo on two objects, but does some special corner case checking, converting, etc.

Originally I wrote the function as such:

public static bool BetterCompare(IComparable lhs, IComparable rhs, out retCode)
{
    ...
    retCode = lhs.CompareTo(rhs);
    ...
}

But the problem is that if I have a class AwesomeClass : IComparable<AwesomeClass>. In fact I have several as some older IComparable classes have gone IComparable<T>. Yet the compiler gets angry because it can't convert these new objects to IComparable. I don't know if this makes it worse, but some of them are abstract (though the abstract class does provide an implementation).

How can I convey "I want two objects that I can call CompareTo on" and not have the compiler give me any lip. Preferably, the new function should NOT look like BetterCompare<AwesomeClass>(this, that, out retCode);, but just "do the right thing". Or is there a better way to do this without touching every class making them both IComparable and IComparable<T>?

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

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

发布评论

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

评论(4

青瓷清茶倾城歌 2024-08-01 14:22:50

您可以使 AwesomeClass 实现非泛型 IComparable 接口以及 IComparable,也可以编写泛型 BetterCompare< /code> 方法:(

public static bool BetterCompare<T>(T lhs, T rhs, out retCode)
    where T : IComparable<T>
{
    ...
    retCode = lhs.CompareTo(rhs);
    ...
}

请注意,这可以与您现有的方法一起使用。)

You can either make AwesomeClass implement the nongeneric IComparable interface as well as IComparable<T>, or you can write a generic BetterCompare method:

public static bool BetterCompare<T>(T lhs, T rhs, out retCode)
    where T : IComparable<T>
{
    ...
    retCode = lhs.CompareTo(rhs);
    ...
}

(Note that this can live alongside your existing method.)

肩上的翅膀 2024-08-01 14:22:50

您始终可以将变量更改为对象类型,然后使用反射来查看它们是否实现其中一个接口,然后将它们转换为正确的接口。

注意:反射速度非常慢,因此请谨慎使用。

You could always change the variables to an object type and then use reflection to see if they implement one of the interfaces and then cast them to the correct interface.

NOTE: Reflection is crazy slow, so use it with caution.

行雁书 2024-08-01 14:22:50

You could have a look at Comparer<T>, that implements both IComparer<T> and IComparer, and use that in the appropriate places. It may require some refactoring of your code though.

塔塔猫 2024-08-01 14:22:50

文档 说:

Default 属性返回的对象使用 < code>System.Collections.Generic.IComparable用于比较两个对象的通用接口。 如果类型 T 未实现 IComparable 泛型接口,则 Default 属性将返回使用 System.Collections.IComparableComparer代码>接口。

The documentation says:

The object returned by the Default property uses the System.Collections.Generic.IComparable<T> generic interface to compare two objects. If type T does not implement the IComparable<T> generic interface, the Default property returns a Comparer<T> that uses the System.Collections.IComparable interface.

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