如何混合/修复 IComparable 和 IComparable混乱
我有一个辅助函数,它基本上在两个对象上调用 CompareTo,但会进行一些特殊的极端情况检查、转换等。
最初我是这样编写函数的:
public static bool BetterCompare(IComparable lhs, IComparable rhs, out retCode)
{
...
retCode = lhs.CompareTo(rhs);
...
}
但问题是,如果我有一个 class AwesomeClass : IComparable< ;AwesomeClass>
。 事实上,我有几个,因为一些旧的 IComparable
类已经消失了 IComparable
。 然而编译器会生气,因为它无法将这些新对象转换为 IComparable
。 我不知道这是否会让情况变得更糟,但其中一些是抽象的(尽管抽象类确实提供了实现)。
我如何表达“我想要两个可以调用 CompareTo 的对象”而不让编译器给我任何提示。 最好,新函数不应看起来像 BetterCompare
,而只是“做正确的事”。 或者是否有更好的方法来做到这一点,而无需触及每个类,使它们同时成为 IComparable
和 IComparable
?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使
AwesomeClass
实现非泛型IComparable
接口以及IComparable
,也可以编写泛型BetterCompare< /code> 方法:(
请注意,这可以与您现有的方法一起使用。)
You can either make
AwesomeClass
implement the nongenericIComparable
interface as well asIComparable<T>
, or you can write a genericBetterCompare
method:(Note that this can live alongside your existing method.)
您始终可以将变量更改为对象类型,然后使用反射来查看它们是否实现其中一个接口,然后将它们转换为正确的接口。
注意:反射速度非常慢,因此请谨慎使用。
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.
您可以查看
比较器
< /a>,它实现了IComparer
和IComparer
,并在适当的地方使用它。 不过,它可能需要对您的代码进行一些重构。You could have a look at
Comparer<T>
, that implements bothIComparer<T>
andIComparer
, and use that in the appropriate places. It may require some refactoring of your code though.文档 说:
Default 属性返回的对象使用 < code>System.Collections.Generic.IComparable用于比较两个对象的通用接口。 如果类型 T 未实现 代码>接口。
IComparable
泛型接口,则 Default 属性将返回使用System.Collections.IComparable
ComparerThe 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 theIComparable<T>
generic interface, the Default property returns aComparer<T>
that uses theSystem.Collections.IComparable
interface.