C#没有预处理,我不想为需要此约束的所有接口定义“struct,IComparable,IFormattable,IConvertible”。我需要的是:
命名约束“IMyItemConstraint”,用于几个通用接口where-clauses:
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子句定义命名约束?
答案 0 :(得分:1)
不幸的是,你无法继承通用约束。您需要为每个类型参数单独定义约束。