Nullable<int>
可能无法实现IComparable
,但肯定int
一样。并且Nullable<T>
总是框到T
(例如,当您投射到界面时,如IComparable
,这是一个装箱转换)。所以比较/排序可空属性应该不成问题。
int? value = 1;
IComparable comparable = value; // works; even implicitly
因此,顶部样本中的检查不起作用。试试这个:
Type interfaceType = prop.PropertyType.GetInterface("IComparable");
// Interface not found on the property's type. Maybe the property was nullable?
// For that to happen, it must be value type.
if (interfaceType == null && prop.PropertyType.IsValueType)
{
Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
// Nullable.GetUnderlyingType only returns a non-null value if the
// supplied type was indeed a nullable type.
if (underlyingType != null)
interfaceType = underlyingType.GetInterface("IComparable");
}
if (interfaceType != null)
// rest of sample
还有一个另外:如果你想空值的工作以及(字符串和可空类型),你可以试试这个重新实现SortCore(...)
:
protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
IEnumerable<MyClass> query = base.Items;
if (direction == ListSortDirection.Ascending)
query = query.OrderBy(i => prop.GetValue(i));
else
query = query.OrderByDescending(i => prop.GetValue(i));
int newIndex = 0;
foreach (MyClass item in query)
{
this.Items[newIndex] = item;
newIndex++;
}
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
有没有需要直接查找IComparable
,只需让排序方法自己排序即可。
非常感谢。我还需要设置_sortPropertyValue = prop; _sortDirectionValue =方向; _isSortedValue = true; 为了代码工作。非常感谢:) – David 2009-08-12 13:43:04
重新“对可空属性进行比较/排序不应该是一个问题。” - 除非该属性的值为null,否则您将得到一个运行时异常。 (我假设 - 我没有尝试过。) – ToolmakerSteve 2016-11-02 19:24:56
@ToolmakerSteve,'OrderBy'和'OrderByDescending'不介意属性何时返回null。它使用的默认比较器将排序顶部的'null'值。只有当列表本身包含一个'null'条目时,才会出现问题。 – Ruben 2016-11-04 10:09:37