我遇到了一个与通用接口合同有关的问题。我有两个通用接口,每个接口都有一个单一方法,该方法具有单一前提条件(
值得注意的是,
编辑:正如Porges指出,合同正在编写(通过IL确认),因此这似乎是特定于代码合同编辑器扩展的。
Requires
合同)。第一个接口的合同按预期工作:前提条件被传播到实现类,并且接口方法通过代码合同编辑器扩展适当地装饰。第二个接口的合同没有被检测到,但是两个接口/合同对之间的代码几乎相同。//
// Example working as expected
//
[ContractClass(typeof(IExporterContract<>))]
public interface IExporter<in TInput>
where TInput : class
{
// Shows adornment "requires obj != null"; contracts propogate
void Export(TInput obj);
}
[ContractClassFor(typeof(IExporter<>))]
abstract class IExporterContract<TInput> : IExporter<TInput>
where TInput : class
{
public void Export(TInput obj)
{
Contract.Requires(obj != null);
}
}
//
// Example with unexpected behavior
//
[ContractClass(typeof(IParserContract<>))]
public interface IParser<out TOutput>
where TOutput : class
{
// Workbook is Microsoft.Office.Interop.Excel.Workbook
// Does not show adornment "requires workbook != null"; contracts do not propogate
TOutput Parse(Workbook workbook);
}
[ContractClassFor(typeof(IParser<>))]
abstract class IParserContract<TOutput> : IParser<TOutput>
where TOutput : class
{
public TOutput Parse(Workbook workbook)
{
Contract.Requires(workbook != null);
return default(TOutput);
}
}
值得注意的是,
Microsoft.Office.Interop.*
中的任何接口都会导致此行为。使用任何其他类型,一切都按预期工作。我不知道这是为什么。编辑:正如Porges指出,合同正在编写(通过IL确认),因此这似乎是特定于代码合同编辑器扩展的。