1 /// <summary>
2 /// CplProduitComparer is used to sort the generic collection of the CplProduit class
3 /// </summary>
4 public class CplProduitComparer : IComparer<CplProduit>
5 {
6 #region Constructor ...
7
8 public CplProduitComparer(string p_propertyName)
9 {
10 //We must have a property name for this comparer to work
11 this.PropertyName = p_propertyName;
12 }
13 #endregion
14 #region Property ...
15
16 private string _propertyName;
17
18 public string PropertyName
19 {
20 get { return _propertyName; }
21 set { _propertyName = value; }
22 }
23 #endregion
24 #region IComparer<CplProduit> Members ...
25
26
27 /// <summary>
28 /// This comparer is used to sort the generic comparer
29 /// The constructor sets the PropertyName that is used
30 /// by reflection to access that property in the object to
31 /// object compare.
32 /// </summary>
33 /// <param name="x"></param>
34 /// <param name="y"></param>
35 /// <returns></returns>
36 public int Compare(CplProduit x, CplProduit y)
37 {
38 Type t = x.GetType();
39 PropertyInfo val = t.GetProperty(this.PropertyName);
40 if (val != null)
41 {
42 return Comparer.DefaultInvariant.Compare(val.GetValue(x, null), val.GetValue(y, null));
43 }
44 else
45 {
46 throw new Exception(this.PropertyName + " is not a valid property to sort on. It doesn't exist in the Class.");
47 }
48 }
49 #endregion
50 }