.net Sorting a Value Class Using IComparable

I ran into a problem using a DataGridView binding to an iBindingListView implementation (third-party dll) attached to a large collection.

My collection type has a specific property called MyDateTime , which is a value class similar to DateTime, but also with some legacy code.

This structure implements iComparable , iComparable<T> and iEquatable<T> .

I have a problem:

When I apply sorting to an iBindingListView on MyDateTimeColumn , it ALWAYS uses a non-generic iComparer , causing hundreds of thousands of unnecessary boxes and unpacking.

When I use the automatic sorting provided by DGV, it sorts the row in the column. Saving this column “automatically” instead of programmatically for that column alone would be unacceptable.

When I delete non-generic iComparer, the shared file is still not used, it just compares the string with .ToString ().

Am I missing something? Why is my generic non bieng counterpart causing sorting?

+4
source share
2 answers

Ultimately, this type of data binding is often based on reflection, and reflection is based on object ; therefore boxing is a reality. In fact, you can control this when implementing IBindingListView , but it will be a lot of work, and I assume that they just don't (reasonably).

An easier way to do this (I suppose they use) is to trust the PropertyDescriptor by calling GetValue and then using Comparer.Default.Compare(x,y) . After you called GetValue , there is no point not using the object that you already entered (and would then have to be deleted).

And if you do not trust the PropertyDescriptor , you find yourself in a very implementation-specific code that does not support the general appearance of the ComponentModel world (therefore, it will not work on data tables or custom models, etc.).

+1
source

Unfortunately, there is no way around this; at some point, the DataGridView will deal with the value as an object , which means that it must be boxed if it is a value type.

+1
source

Source: https://habr.com/ru/post/1307555/


All Articles