1

C# 没有预处理,我不想为所有需要此约束的接口定义“struct、IComparable、IFormattable、IConvertible”。我需要的是这样的:

几个通用接口 where 子句的命名约束“IMyItemConstraint” :

public interface IProperty2Enum<T>
    : IEnumerable<T>
    where T : IMyItemConstraint { } // <--- here

public interface IMyCollection2<T>
    : IEnumerable<T>, INotifyCollectionChanged, INotifyPropertyChanged 
    where T : IMyItemConstraint { } // <--- here

public interface IMyObservableCollection2<T>
    : IEnumerable<T>, INotifyCollectionChanged, INotifyPropertyChanged 
    where T : IMyItemConstraint { } // <--- here

我试图定义一个名称“IMyItemConstraint”,但出现错误 CS####:

public interface IMyItemConstraint 
    : struct, IComparable, IFormattable, IConvertible { } // CS1031: Type expected 

public interface IMyItemConstraint : where IMyItemConstraint
    : struct, IComparable, IFormattable, IConvertible { } // CS1031: Type expected 

public interface IMyItemConstraint<T> where T // This does not help:
    : struct, IComparable, IFormattable, IConvertible { }

是否可以为多个接口(合同)的通用接口 where 子句定义命名约束?

4

1 回答 1

2

不幸的是,您不能继承通用约束。您需要为每个类型参数分别定义约束。

于 2017-04-12T21:11:42.970 回答