You gotta love Generics in .Net 2.0. I had to do some Object-sorting in memory with some List<T> stuff.
/// <summary>/// Compare objects of type T using a certain property/// </summary>/// <typeparam name="T"></typeparam>public class PropertyComparison<T> : System.Collections.Generic.IComparer<T>{/// <summary>/// What property of the objects to use to compare one to another/// </summary>/// <param name="property"></param>public PropertyComparison(string property){_propName = property;}private string _propName;private PropertyInfo _prop;public int Compare(T x, T y){if (null == _prop){_prop = x.GetType().GetProperty(_propName);}try{IComparable xVal = _prop.GetValue(x, null) as IComparable;IComparable yVal = _prop.GetValue(y, null) as IComparable;return xVal.CompareTo(yVal);}catch (System.NullReferenceException){throw new ApplicationException("Type " + _prop.PropertyType.ToString() + " is not IComparable");}}}
So then I would use it:
if (!string.IsNullOrEmpty(prop)){CarSpot.Mobile.Types.PropertyComparison<Vehicle> c = new CarSpot.Mobile.Types.PropertyComparison<Vehicle>(prop);try{_dataSource.Sort(c);
Remember Me
a@href@title, strike
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Damon Payne
E-mail