I
INeedADip
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 'GenericComparer' to type
'System.Collections.Generic.IComparer `1[AccountDB.Queue]'
My code looks like the following:
List<AccountDB.Queue> oList = getAllQueuesFunction();
oList.Sort(New GenericComparer("QueueName"));
This is my GenericComparer:
public class GenericComparer : IComparer
{
string propertyName;
public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}
public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);
// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);
// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}
Hopefull you can see where I'm trying to go with this. Any
suggestions? Am I approaching this the wrong way?
ICamparer parameter for a generic list..but I get the error:
"Unable to cast object of type 'GenericComparer' to type
'System.Collections.Generic.IComparer `1[AccountDB.Queue]'
My code looks like the following:
List<AccountDB.Queue> oList = getAllQueuesFunction();
oList.Sort(New GenericComparer("QueueName"));
This is my GenericComparer:
public class GenericComparer : IComparer
{
string propertyName;
public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}
public int Compare(object x, object y)
{
// gets the value of the x property
PropertyInfo property =
x.GetType().GetProperty(propertyName);
object valueOfX = property.GetValue(x, null);
// gets the value of the y property
property = y.GetType().GetProperty(propertyName);
object valueOfY = property.GetValue(y, null);
// now makes the comparsion
return ((IComparable)valueOfX).CompareTo(valueOfY);
}
}
Hopefull you can see where I'm trying to go with this. Any
suggestions? Am I approaching this the wrong way?