code合同:我如何提供一个合同类的通用接口? [英] Code Contracts: How do I supply a contract class for a generic interface?

查看:183
本文介绍了code合同:我如何提供一个合同类的通用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想指定的合同这个通用接口,使用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<>))] 

这篇关于code合同:我如何提供一个合同类的通用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆