IComparer问题 [英] IComparer Question
问题描述
我正在尝试使用通用(反射)IComparer类来对
泛型列表进行排序,但是我收到错误:
无法转换类型''的对象GenericComparer''键入
''System.Collections.Generic.Icomparer`1 [AccountDB.Queue]''
我的代码如下所示:
列表< AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer(" QueueName")); < - 错误
我的班级如下:
公共类GenericComparer:IComparer
{
string propertyName;
public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}
public int比较(对象x,对象y)
{
//获取x属性的值
PropertyInfo property =
x.GetType()。GetProperty(propertyName);
object valueOfX = property。 GetValue(x,null);
//获取y属性的值
property = y.GetType()。GetProperty(propertyName);
对象valueOfY = property.GetValue(y,null);
//现在进行比较
return((IComparable) valueOfX).CompareTo(valueOfY);
}
}
希望你能看到我想要完成的任务。这是错误的接近
吗?有任何建议或意见吗?
I am trying to use a generic (reflection) IComparer class to sort 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();
List.Sort(New GenericComparer("QueueName")); <-- Error
My class looks like:
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);
}
}
Hopefully you can see what I am trying to accomplish. Am I approaching
this the wrong way? Any suggestions or Comments?
推荐答案
INeedADip< in ******* @ gmail.com>写道:
INeedADip <in*******@gmail.com> wrote:
我试图使用通用(反射)IComparer类来排序
通用列表,但我得到错误:
无法转换类型为''GenericComparer的对象''键入
''System.Collections.Generic.Icomparer`1 [AccountDB.Queue]''
我的代码如下所示:
List< ; AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer(" QueueName")); < - 错误
I am trying to use a generic (reflection) IComparer class to sort 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();
List.Sort(New GenericComparer("QueueName")); <-- Error
您已经实现了System.Collections.IComparer。你需要实现
System.Collections.Generic.IComparer< T> ;.
-
Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget 博客: http://www.msmvps.com/ jon.skeet
如果回复小组,请不要给我发邮件
You''ve implemented System.Collections.IComparer. You need to implement
System.Collections.Generic.IComparer<T>.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
您收到此错误,因为您的GenericComparer实现IComparer而不是
IComparer< T>。尽管名称相同,但两个界面都不同。
如果查看List< T>的Sort方法。通用你会看到它是
声明为
public void排序(
IComparer< T> comparer
)您需要声明比较器aspublic类GenericComparer:
IComparer< [列表中元素的类型]>或者您可以创建通用
comparer并在创建时指定元素的类型.-- HTHStoitcho
Goutsev(100)" INeedADip" <在******* @ gmail.com>在消息中写道
news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
Hi,
You get this error because your GenericComparer implements IComparer not
IComparer<T>. Despite the same name both interfaces are different.
If you look at the Sort method of List<T> generic you''ll see that it is
declared as
public void Sort (
IComparer<T> comparer
)You need to declare you comparer aspublic class GenericComparer :
IComparer<[the type of the elements in the list]>or you can create generic
comparer and specify the type of the elements upon creation.-- HTHStoitcho
Goutsev (100)"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
我正在尝试使用通用(反射)IComparer类来对
通用列表进行排序,但是我得到错误:
无法将GenericComparer类型的对象强制转换为类型
''System.Collections.Generic.Icomparer`1 [AccountDB.Queue]''
我的代码如下所示:
List< AccountDB.Queue> oList = getAllQueuesFunction();
List.Sort(New GenericComparer(" QueueName")); < - 错误
我的课程如下:
公共类GenericComparer:IComparer
{
string propertyName;
> public GenericComparer(string propertyName)
{
this.propertyName = propertyName;
}
public int Compare(object x,object y)
{
//获取x属性的值
PropertyInfo property =
x.GetType()。GetProperty(propertyName);
object valueOfX = property.GetValue(x,null) ;
//获取y属性的值
property = y.GetType()。GetProperty(propertyName);
object valueOfY = property.GetValue(y,null) ;
//现在进行比较
返回((IComparable)valueOfX)。比较(valueOfY);
}
}
希望你能看到我想要完成的任务。我接近这是错误的方式吗?有什么建议或评论吗?
I am trying to use a generic (reflection) IComparer class to sort 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();
List.Sort(New GenericComparer("QueueName")); <-- Error
My class looks like:
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);
}
}
Hopefully you can see what I am trying to accomplish. Am I approaching
this the wrong way? Any suggestions or Comments?
关于如何处理这个的任何想法?
我想也许是IComparer<对象>但是我不能完全开始工作..
Any ideas on how I should approach this?
I thought maybe IComparer<object> but I can''t quite get it to work..
这篇关于IComparer问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!