I am trying to use the following generic (reflection) class as the
ICamparer parameter for a generic list..but I get the error:
"Unable to cast object of type 'GenericCompare r' to type
'System.Collect ions.Generic.IC omparer `1[AccountDB.Queue]'
My code looks like the following:
List<AccountDB. Queue> oList = getAllQueuesFun ction();
oList.Sort(New GenericComparer ("QueueName" ));
This is my GenericComparer :
public class GenericComparer : IComparer
{
string propertyName;
public GenericComparer (string propertyName)
{
this.propertyNa me = propertyName;
}
public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().Get Property(proper tyName);
object valueOfX = property.GetVal ue(x, null);
// gets the value of the y property
property = y.GetType().Get Property(proper tyName);
object valueOfY = property.GetVal ue(y, null);
// now makes the comparsion
return ((IComparable)v alueOfX).Compar eTo(valueOfY);
}
}
Hopefull you can see where I'm trying to go with this. Any
suggestions? Am I approaching this the wrong way?