code合同:我如何提供一个合同类的通用接口?提供一个、同类、接口、合同

由网友(丶秋雨涩)分享简介:我想指定的合同这个通用接口,使用code合同:I'd like to specify a contract for this generic interface, using Code Contracts:interface IRandomWriteAccessible{T this[uint index]...

我想指定的合同这个通用接口,使用code合同:

I'd like to specify a contract for this generic interface, using Code Contracts:

interface IRandomWriteAccessible<T>
{
    T this[uint index] { set; }
    uint Length { get; }
}

该文件说,指定合约的接口时,使用 ContractClass 属性。但是,编译器会抱怨这样的:

The documentation says to use the ContractClass attribute when specifying a contract for an interface. However, the compiler will complain about this:

[ContractClass(typeof(IRandomWriteAccessibleContract<T>))]
//             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^     <-- compiler error
interface IRandomWriteAccessible<T> { … }

[ContractClassFor(typeof(IRandomWriteAccessible<T>))]
//                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^          <-- compiler error
sealed class IRandomWriteAccessibleContract<T> : IRandomWriteAccessible<T> { … }

似乎类型参数不能用于具有属性

It seems that type parameters cannot be used with attributes.

如何的不的我写了一个合同,我的通用接口?或者,这是不可能的code合同?

How do I write a contract for my generic interface? Or is this not possible with Code Contracts?

推荐答案

正如在这个问题的其他意见,你应该从你的属性使用删除泛型类型标识符,因为它不能在编译时解析:

As mentioned by other comments in this question, you should remove the generic type identifier from your attribute usage as it can not be resolved at compile time:

[ContractClass(typeof(IRandomWriteAccessibleContract<>))] 
阅读全文

相关推荐

最新文章