无法阵列中比较两个元件 [英] Failed to compare two elements in the array

查看:227
本文介绍了无法阵列中比较两个元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名单,其中,T> ,其中 T 是暴露了一个用户名属性的类。用户名是封装了一个字符串一个自定义类型。我实现了 IComparable的< T> 上简单地返回这个自定义类型的接口

I have a List<T> where T is a class that exposes a "Username" property. Username is of a custom type that encapsulates a string. I implemented the IComparable<T> interface on this custom type that simply returns

this.encapsulatedString.CompareTo(other.encapsulatedString)

我定义的的ICollectionView 的名单中是这样的:

I defined an ICollectionView of the List thus:

AllUsers=CollectionViewSource.GetDefaultView(myList);

我添加了一个Sortdescription:

I added a Sortdescription:

AllUsers.SortDescriptions.Add(new SortDescription("Username",ListSortDirection.Ascending));

在此行的code抛出的称号规定的除外。我可以排序列表中通过其他方式没有问题。哪里是例外来自哪里?

On this line the code throws the exception stated in the title. I can sort the list by other means without problem. Where is the exception coming from?

推荐答案

Stupidstupidstupid:自定义类型有落实 IComparable的以及 IComparable的&LT; T&GT; 看来SortDescription使用的CompareTo的老式的非通用版本

Stupidstupidstupid: The custom type has to implement IComparable as well as IComparable<T> It seems the SortDescription uses the old fashioned non-generic version of CompareTo

我会得到一些急需的睡眠......

I am going to get some much needed sleep...

这篇关于无法阵列中比较两个元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆