自定义通用比较集合






2.33/5 (2投票s)
2006年12月20日

24881

151
一个泛型类,用于通过对象的某个属性进行比较。
引言
一个泛型类,用于通过对象的某个属性进行比较。
背景
我想要一种方法来根据属性对自定义集合进行排序。
使用代码
这是一个非常基本的实现,包含一个基本的 Person 类和一个 Person 对象集合。在集合的排序方法中,调用了基础比较类的一个新实例。该方法使用字段名(属性名)、集合中包含的类类型以及排序方向。基础比较类使用反射来查找属性,然后检索属性值并进行比较。
当您想要实现绑定到自定义集合的数据网格的排序时,这会非常有用。
<CODE> using System; using System.Collections; using System.Reflection; // // This is a generic class, used for comparing an object by one of it's properites // public class BaseClassComparer : IComparer { #region Private Members private int _dir =1; private PropertyInfo propInfo; #endregion // // This is a generic class, used for comparing an object by one of it's properites. // will throw ArgumentException property is not part of object. // // key = This is the name of the properites to sort by // objType = Type of object compared> // direction =1 for ascending (default) -1 for descending public BaseClassComparer( string key , Type objType , int direction ) { setComparer( key, objType , direction ); } public BaseClassComparer( string key, Type objType ) { setComparer( key, objType ,1 ); } #region Public Methods public int Compare( object x , object y ) { IComparable a = ( IComparable ) propInfo.GetValue( x , null ); return ( a.CompareTo( propInfo.GetValue( y , null ) ) * _dir ); } #endregion #region Private Methods private void setComparer( string key , Type objType , int direction ) { if ( direction.Equals( -1 ) ) _dir = -1; propInfo = objType.GetProperty( key ); if ( propInfo == null ) throw new ArgumentException( "key" , "The property does not exist." ); } #endregion } // Basic Person object public class Person { private int _internalId = 0; private string _firstName = string.Empty ; private string _lastName = string.Empty; private string _streetAddress = string.Empty; private string _city = string.Empty; private string _zip = string.Empty; private string _state = string.Empty; public Person () {} public string firstName { get{ return _firstName; } set{ _firstName = value; } } public string lastName { get{ return _lastName; } set{ _lastName = value; } } public string streetAddress { get{ return _streetAddress; } set{ _streetAddress = value; } } public string city { get{ return _city; } set{ _city = value; } } public string zip { get{ return _zip; } set{ _zip = value; } } public DateTime lastLoan { get{ return _lastLoan; } set{ _lastLoan = value; } } public int internalId { get { return _internalId; } set { _internalId = value; } } } // Basic Collection of Person objects public class Persons : CollectionBase { public Person this[ int index ] { get { return( ( Person ) List[ index ] ); } set { List[ index ] = value; } } public int Add( Person obj ) { return( List.Add( obj) ); } public int IndexOf( Person obj ) { return( List.IndexOf( obj ) ); } public void Insert( int index , Person obj ) { List.Insert( index, obj ); } public void Remove( Person obj ) { List.Remove( obj ); } public bool Contains( Person obj ) { return( List.Contains( obj ) ); } protected override void OnValidate( Object obj ) { if ( objDJ.GetType() !=new DJ().GetType() ) throw new ArgumentException( "Object must be a Person." , "value" ); } public void sort( string field , int dir) { // Use of the BaseClassComparer // key parm(1) - This is the name of the properites to sort by // objType parm(2) - Type of object compared // direction parm(3) - 1 for ascending (default) -1 for descending InnerList.Sort( new BaseClassComparer( field , this[ 0 ].GetType() , dir ) ); } }
关注点
在 2.0 版本中引入泛型后会容易得多。我发现这是一种方便的解决方案,用于对绑定到集合的数据网格进行排序。
历史
目前没有更新。