IComparer< T> child作为通用参数不能在Sort中使用 [英] IComparer<T> child as a generic param could not be used in Sort

查看:80
本文介绍了IComparer< T> child作为通用参数不能在Sort中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试用C#对我的List<>进行排序时,卡住了.

Got stuck trying to sort my List<> in C#.

我有一个接口类型及其实现类:

I have an interface type with its implementation class:

public interface IRoom
{
    // ...
}

public class Room : IRoom
{
    // ...
}

还有一个比较器的基类:

And a base class for my comparers:

public abstract class RoomComparer : IComparer<IRoom>
{
    public RoomComparer()
    {
    }

    public abstract int Compare(IRoom x, IRoom y);
}

及其两个孩子:

public class StandardMultipliedSquareMetersComparer : RoomComparer
{
    public StandardMultipliedSquareMetersComparer()
    {
    }

    public override int Compare(IRoom a, IRoom b)
    {
        // ...
    }
}

public class SquareMetersComparer : RoomComparer
{
    public SquareMetersComparer()
    {
    }

    public override int Compare(IRoom a, IRoom b)
    {
        // ...
    }
}

现在,问题就从这里开始:我得到了一个通用类Hotel,其中包含我的Room实例列表:

Now, here's where the problems begin: I got a generic class Hotel with a list of my Room instances:

public class Hotel<TRoom, TComparer> : IHotel, IEnumerable<TRoom> 
    where TRoom : IRoom 
    where TComparer : RoomComparer, new()
{
    public List<TRoom> Rooms;
    protected TComparer Comparer;

    public Hotel(TRoom[] rooms)
    {
        Rooms = new List<TRoom>();
        Rooms.AddRange(rooms);
        Comparer = new TComparer();
        Rooms.Sort(Comparer);
    }
}

这是麻烦所在-我在Rooms.Sort(Comparer);行上遇到两个错误:

And here's the trouble - I got two errors on the line Rooms.Sort(Comparer);:

错误CS1502:最佳重载方法匹配 System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer) 有一些无效的参数(CS1502)

Error CS1502: The best overloaded method match for `System.Collections.Generic.List.Sort(System.Collections.Generic.IComparer)' has some invalid arguments (CS1502)

错误CS1503:自变量#1' cannot convert TComparer'表达式 类型"System.Collections.Generic.IComparer"(CS1503)

Error CS1503: Argument #1' cannot convertTComparer' expression to type `System.Collections.Generic.IComparer' (CS1503)

我尝试了许多不同的解决方案,但没有结果.发生什么事了?

I tried many different solutions, but no result. What's happening?

注意:在Ubuntu上使用Mono 3.10.0

Note: using Mono 3.10.0 on Ubuntu

推荐答案

您正在尝试为此使用一般的协方差(如果比较器可以比较两个TRoom值,则它应该能够比较两个IRoom值),通常 很好-但仅当已知TRoom是类类型时才可以.您可以通过要求TRoom是引用类型来解决此问题:

You're trying to use generic contravariance for this (if a comparer can compare two TRoom values, it should be able to compare two IRoom values), which would usually be fine - but only when TRoom is known to be a class type. You can fix that by requiring that TRoom is a reference type:

where TRoom : class, IRoom

在这一点上,反差很好,一切都很好.当然,这确实要求您所有的房型都是真正的教室.

At that point, contravariance works fine, and all is well. It does require that all your room types are genuinely classes, of course.

您肯定需要像这样的酒店吗?在我看来,这有点太过分了……

Do you definitely need the hotel to be generic like this? It seems a little too much like overkill to me...

这篇关于IComparer&lt; T&gt; child作为通用参数不能在Sort中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆