5

我在泛型和继承方面遇到了一些麻烦。我有一个抽象类CriteriaBase,它的工作是确定一个实体是否T符合任何子类中定义的标准。子类必须实现一个返回Func代表标准的方法。当我尝试将泛型用于Func. 希望一些代码能说明我的问题。

public abstract class CriteriaBase<T, U>
    where T : ICrossoverable
    where U : IChromosome
{
    protected abstract Func<U, bool> Criteria { get; }
    //some code removed for brevity

    private int GetNumberOfCriteriaMatches(T season)
    {
        //1.  works
        //Func<IChromosome, bool> predicate = c => c.Genes == null;
        //return season.Chromosomes.Count(predicate);

        //2.  doesn't work - The type arguments for method 'int 
        //System.Linq.Enumerable.Count<TSource>(this IEnumerable<TSource>, 
        //Func<TSource, bool>)'
        //cannot be inferred from the usage. Try specifying the type arguments 
        //explicitly.
        return season.Chromosomes.Count(Criteria);
    }
}

我的意图是CriteriaBase该类应该是通用的并且完全可重用。

一个示例子类:

public class TopTeamsPlayingEachOtherCriteria : CriteriaBase<Season, MatchDay>
{
    //some code removed for brevity

    protected override Func<MatchDay, bool> Criteria
    {
        get { return matchDay =>
            matchDay.Fixtures.Count(
                fixture =>
                fixture.HomeTeam.TableGrouping.Ordering == 1 
                && fixture.AwayTeam.TableGrouping.Ordering == 1) > 1; }
    }
}

问题出在GetNumberOfCriteriaMatches()方法上。选项 2 是我最初编写代码的方式,但我得到了列出的编译错误。如果我使用选项 1,那么代码会编译,但这意味着当我Criteria在子类中覆盖时,我必须使用IChromosome而不是使用MatchDay哪个不起作用(我需要访问 a 的特定功能MatchDay)。在我看来,选项 1 和 2 是等效的。选项 2 简单地替换为仅限于实现的类IChromosome的泛型类型。UIChromosome

我想要实现的目标是可能的吗?如果是这样,我错过了什么/误解了什么?如果没有,我应该如何解决这个问题?

为了完整性(包括在最后,因为我不确定它对这个问题有多大帮助),这是我目前用于T( Season) 和U( MatchDay) 的两个实体。

public class Season : ICrossoverable
{
    private readonly IEnumerable<MatchDay> _matchDays;

    public Season(IEnumerable<MatchDay> matchDays)
    {
        _matchDays = matchDays;
    }

    public IEnumerable<MatchDay> MatchDays
    {
        get { return _matchDays; }
    }

    //ICrossoverable implementation
    public IEnumerable<IChromosome> Chromosomes
    {
        get { return _matchDays; }
    }
}

public class MatchDay : IChromosome
{
    private readonly int _week;
    private readonly List<Fixture> _fixtures;

    public MatchDay(int week, List<Fixture> fixtures)
    {
        _week = week;
        _fixtures = fixtures;
    }

    //some code removed for brevity

    public IEnumerable<Fixture> Fixtures
    {
        get { return _fixtures; }
    }

    //IChromosome implementation
    public IEnumerable<IGene> Genes
    {
        get { return Fixtures; }
    }
}
4

2 回答 2

8

好吧,这就是问题所在:

public IEnumerable<IChromosome> Chromosomes

只是声明您正在返回一系列IChromosome值。您的标准MatchDay期望值。碰巧知道它实际上是返回一个MatchDay值序列,但编译器没有。

您可以Cast<>在执行时使用它来检查:

return season.Chromosomes.Cast<U>().Count(Criteria);

... or you could change Chromosomes to return an IEnumerable<MatchDay>. Unfortunately we can't really tell whether that's a valid answer or not as we don't know how ICrossoverable is declared. Perhaps you should make ICrossoverable generic in the element type?

于 2013-04-08T18:24:15.253 回答
3

in您应该在 CriteriaBase 定义中的 U 之前使用关键字。像这样的东西:

public abstract class CriteriaBase<T, in U>
    where T : ICrossoverable
    where U : IChromosome

更新。不起作用。尝试明确指定类型

private int GetNumberOfCriteriaMatches(T season)
{
 ....
    return season.Chromosomes.Count<IChromosome>(Criteria);
}
于 2013-04-07T21:41:33.060 回答