类被注释为接口契约,并且不能具有除Object之外的基类 [英] Class is annotated as being an interface contract and cannot have a base class other than Object
问题描述
新版本的代码合同抱怨我的合同类是从它们是合同类型的基类的合同类继承的。我这样做是为了避免重复不必要的方法,我认为应该允许这样做。
The new version of code contracts is complaining that my contract classes are inheriting from the contract class of the base class of the type they are a contract for. I do this to avoid repeating unnecessary methods, and I think it should be allowed.
例如:
'''<summary>An IEnumerable with an efficient method for counting items.</summary>
<ContractClass(GetType(ISizedEnumerableContractClass(Of )))>
Public Interface ISizedEnumerable(Of Out T)
Inherits IEnumerable(Of T)
ReadOnly Property Count As Integer
End Interface
'''<summary>An IEnumerable with an efficient method to get items at given offsets.</summary>
<ContractClass(GetType(IIndexedEnumerableContractClass(Of )))>
Public Interface IIndexedEnumerable(Of Out T)
Inherits ISizedEnumerable(Of T)
Default ReadOnly Property Item(ByVal index As Integer) As T
End Interface
<ContractClassFor(GetType(ISizedEnumerable(Of )))>
Public MustInherit Class ISizedEnumerableContractClass(Of T)
Implements ISizedEnumerable(Of T)
Public ReadOnly Property Count As Integer Implements ISizedEnumerable(Of T).Count
Get
Contract.Ensures(Contract.Result(Of Integer)() >= 0)
Throw New NotSupportedException
End Get
End Property
Public Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator
Throw New NotSupportedException
End Function
Public Function GetEnumeratorObj() As System.Collections.IEnumerator Implements IEnumerable.GetEnumerator
Throw New NotSupportedException
End Function
End Class
<ContractClassFor(GetType(IIndexedEnumerable(Of )))>
Public MustInherit Class IIndexedEnumerableContractClass(Of T)
Inherits ISizedEnumerableContractClass(Of T)
'^^^ Warning Here ^^^
Implements IIndexedEnumerable(Of T)
Default Public ReadOnly Property Item(ByVal index As Integer) As T Implements IIndexedEnumerable(Of T).Item
Get
Contract.Requires(index >= 0)
Contract.Requires(index < DirectCast(Me, IIndexedEnumerable(Of T)).Count)
Throw New NotSupportedException
End Get
End Property
End Class
推荐答案
嗨Strilanc,
Hi Strilanc,
是的,这是一个地方我们开始在允许的范围内限制更多。这是因为需要避免对基本合同中的方法的错误引用模式,我们很难转发到正确的接口方法。
Yes, this is one place where we started being more restrictive in what is allowed. This is prompted by the need to avoid bad reference patterns to the methods in the base contract that we have difficulty forwarding to the proper interface method.
只需让编译器填写缺少的成员即可。它们可以是假人,因为它们从不需要。
Just let the compiler fill in the missing members. They can be dummies as they are never needed.
这篇关于类被注释为接口契约,并且不能具有除Object之外的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!