我有一個數學庫的通用接口,這樣的事情:我應該用輸入代碼合同通用接口?
[ContractClass(typeof(MathsDoubleContracts))]
public interface IMaths<T>
{
T SomeFunction(T n);
}
這使我能夠建立一個IMaths<double>
,IMaths<decimal>
等(雖然起初我只需要double
版)。
我想設置代碼合同。目前我已經做到了這一點:
[ContractClassFor(typeof(IMaths<double>))]
public abstract class MathsDoubleContracts : IMaths<double>
{
public double SomeFunction(double n)
{
// Always positive
Contract.Ensures(0 <= Contract.Result<double>());
return double.NaN;
}
}
這似乎是工作,但我很吃驚的是它(因爲我在IMaths<double>
而非IMaths<T>
指定的合同)。
我在想:
- 我可以一個通用的接口上指定多個合同類,每一個特定的類型我想要使用(例如同時擁有
[ContractClass(typeof(MathsDoubleContracts))]
和IMaths<T>
[ContractClass(typeof(MathsDecimalContracts))]
屬性)?這是明智的做法嗎? - 我會更好而不是使用所有的通用接口(即開始,比如說,所有的功能都定義爲雙打,後面加入
IMathsDecimal
),比如說IMathsDouble
?
感謝您的回答。我澄清了我在第一季的含義。 – 2012-01-05 10:16:04
乾杯更新了答案。 – Aliostad 2012-01-05 10:22:20