Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以约束一个模板参数类来实现其他类所支持的接口吗?_C#_Generics_Interface - Fatal编程技术网

C# 我可以约束一个模板参数类来实现其他类所支持的接口吗?

C# 我可以约束一个模板参数类来实现其他类所支持的接口吗?,c#,generics,interface,C#,Generics,Interface,名称有点模糊,因此情况如下: 我正在编写代码来使用一些“轨迹”。轨迹是一个抽象的东西,所以我用不同的接口来描述它们。所以我有一个代码如下: namespace Trajectories { public interface IInitial<Atom> { Atom Initial { get; set; } } public interface ICurrent<Atom> { Atom Curr

名称有点模糊,因此情况如下:

我正在编写代码来使用一些“轨迹”。轨迹是一个抽象的东西,所以我用不同的接口来描述它们。所以我有一个代码如下:

namespace Trajectories {
    public interface IInitial<Atom>
    {
        Atom Initial { get; set; }
    }

    public interface ICurrent<Atom>
    {
        Atom Current { get; set; }
    }

    public interface IPrevious<Atom>
    {
        Atom Previous { get; set; }
    }

    public interface ICount<Atom>
    {
        int Count { get; }
    }

    public interface IManualCount<Atom> : ICount<Atom>
    {
        int Count { get; set; }
    }
    ...
}

我认为,如果我为每个接口子集创建一个整体接口,我可以做到,但它会变得非常难看。

您可以完全消除
I条件
,只使用谓词和函数样式。下面是一个静态方法示例:-

public static Predicate<T> CountLessThan<T> (int value) where T:ICount
{ 
    return (T item) => item.Count < value;
}

在T中使用泛型将确保您只能组合具有相同T的条件。

您可以完全消除
ICondition
,只使用谓词和函数样式。下面是一个静态方法示例:-

public static Predicate<T> CountLessThan<T> (int value) where T:ICount
{ 
    return (T item) => item.Count < value;
}

T中的泛型将确保您只能组合具有相同T的条件。

顺便说一句,您不需要通过引用发送轨迹(使用
ref
关键字),只要轨迹是一个类(引用类型),其中
轨迹:Trajectories.ICount
但ICount不是泛型接口,这是为什么?它是一个通用接口:
公共接口ICount{}
,但我认为它不应该是。因为不需要-它不使用
Atom
类型参数。是的,没错,ICount不应该是泛型的。顺便说一句,只要轨迹是类(引用类型),就不需要通过引用发送轨迹(使用
ref
关键字)你有where
轨迹:Trajectories.ICount
但是ICount不是一个通用接口,为什么呢?它是一个通用接口:
公共接口ICount{}
但我认为它不应该是。因为不需要-它不使用
Atom
类型参数。是的,没错,ICount不应该是泛型的。但问题仍然存在:CountLessThan需要t:ICount,而currentNormalssthan需要t:icCurrent,所以它们是不同的t。您的代码将如下所示:谓词和(首先是谓词,其次是谓词),其中T1:ICount,其中T2:ICurrent,其中两者:ICount,ICurrent;And应仅接受实现两个参数所需接口并集的类。
如果实际有一个实现这两个接口的
Both
类,但是您想要的是一个谓词,它可以与BothA或BothB一起使用,前提是它们实现了构建And谓词中提到的两个接口,对吗?不是一个只与两个
绑定的
。我明白了……对不起,
并不意味着要在上面有接口,它只需要两个相同的
t
谓词就可以工作。修正了。但问题仍然存在:CountLessThan需要T:ICount,而currentNormalsthan需要T:icCurrent,所以它们是不同的T。您的代码将如下所示:谓词和(首先是谓词,其次是谓词),其中T1:ICount,其中T2:ICurrent,其中两者:ICount,ICurrent;And应仅接受实现两个参数所需接口并集的类。
如果实际有一个实现这两个接口的
Both
类,但是您想要的是一个谓词,它可以与BothA或BothB一起使用,前提是它们实现了构建And谓词中提到的两个接口,对吗?不是一个只与两个
绑定的
。我明白了……对不起,
并不意味着要在上面有接口,它只需要两个相同的
t
谓词就可以工作。固定的。
public class And<Atom, CondA, TrajectoryA, CondB, TrajectoryB, Trajectory> : ICondition<Atom, Trajectory>
    where CondA : ICondition<Atom, TrajectoryA>
    where TrajectoryA : // Some interfaces
    where CondB : ICondition<Atom, TrajectoryB>
    where TrajectoryB : // Some interfaces 
    where Trajectory : // MUST IMPLEMENT THE INTERFACES FOR TrajectoryA AND THE INTERFACES FOR TrajectoryB 
{
    public CondA A { get; set; }
    public CondB B { get; set; }

    public bool Test(ref Trajectory t) {
        return A.Test(t) && B.Test(t);
    }
}
var vand = new CountLessThan(32) & new CurrentNormLessThan(4.0);
public static Predicate<T> CountLessThan<T> (int value) where T:ICount
{ 
    return (T item) => item.Count < value;
}
Predicate<SimpleTrajectory> anded = x => CountLessThan<SimpleTrajectory>(5)(x) && CountLessThan<SimpleTrajectory>(3)(x);
public static Predicate<T> And<T>(this Predicate<T> first, Predicate<T> second)
{
    return (T item) => first(item) && second(item);
}
 ... = new CountLessThan(5).And(new CountLessThan(3))