gpt4 book ai didi

c# - Mono 中的通用 IComparable 实现问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:00 24 4
gpt4 key购买 nike

此代码在 .NET 4.0 中成功执行

    public void CompareTest()
{
var m1 = new Foo { Order = 1 };
var m2 = new Foo { Order = 2 };
var c1 = new Bar { Order = -1 };
var c2 = new Bar { Order = 3 };
var list1 = new List<IOrderable> { m1, m2, c1, c2 };
var list2 = new List<Bar> { c1, c2 };
list1.Sort();
list2.Sort();
}

public interface IOrderable : IComparable<IOrderable>
{
int Order { get; set; }
}

public class Foo : IOrderable
{
public int Order { get; set; }

public int CompareTo(IOrderable other)
{
return Order.CompareTo(other.Order);
}
}

public class Bar : IOrderable
{
public int Order { get; set; }

public int CompareTo(IOrderable other)
{
return Order.CompareTo(other.Order);
}
}

但是在 Mono 2.6(在 Unity3D 中)它抛出一个异常:

ArgumentException: does not implement right interface

它抛出第二个排序 (list2.Sort()) 而不是第一个

我对所有这些比较内容有点陌生,所以如果我在这里遗漏了一些明显的东西,请原谅。

为什么提示没有实现正确的接口(interface)?它被实现。知道这里发生了什么吗?为什么它在 .NET 中工作但在 Mono 中不工作?

谢谢。

编辑:全栈

ArgumentException: does not implement right interface
System.Collections.Generic.Comparer`1+DefaultComparer[ShowEmAll.BetterBehaviourEditor+Bar].Compare (ShowEmAll.Bar x, ShowEmAll.Bar y) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Comparer.cs:86)
System.Array.compare[Bar] (ShowEmAll.Bar value1, ShowEmAll.Bar value2, IComparer`1 comparer) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Array.cs:1744)
System.Array.qsort[Bar,Bar] (.Bar[] keys, .Bar[] items, Int32 low0, Int32 high0, IComparer`1 comparer) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Array.cs:1721)
System.Array.Sort[Bar,Bar] (.Bar[] keys, .Bar[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Array.cs:1674)
Rethrow as InvalidOperationException: The comparer threw an exception.
System.Array.Sort[Bar,Bar] (.Bar[] keys, .Bar[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Array.cs:1677)
System.Array.Sort[Bar] (.Bar[] array, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Array.cs:1623)
System.Collections.Generic.List`1[ShowEmAll.BetterBehaviourEditor+Bar].Sort () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:568)
ShowEmAll.BetterBehaviourEditor.CompareTest () (at Assets/Vexe/ShowEmAll/Core/Editor/CustomEditors/BetterBehaviourEditor.cs:81)
ShowEmAll.BetterBehaviourEditor.OnEnable () (at Assets/Vexe/ShowEmAll/Core/Editor/CustomEditors/BetterBehaviourEditor.cs:62)

最佳答案

首先,这看起来可能是 Comparer<T>.Default 中的一个 Mono 错误,如果 T 应该注意工具 IComparable<Foo>对于一些 Foo那是一个接口(interface) T工具,或 T 的基本类型.

对于解决方法,有四个选项:

  • 实现非通用 IComparable Bar 中的界面,可能只是委托(delegate)给 CompareTo(IOrder) 的实现
  • 实现 IComparable<Bar>Bar ,可能只是委托(delegate)给 CompareTo(IOrder) 的实现
  • 始终使用 List<IOrder>即使其中的每一项都是 Bar
  • 指定 Comparer<T>List<T>.Sort - 我怀疑您可以很容易地使用泛型构建自己的实现。

关于c# - Mono 中的通用 IComparable 实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24610968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com