当方法具有泛型约束时,前提条件不会被继承 [英] Preconditions not being inherited when method has a generic constraint
本文介绍了当方法具有泛型约束时,前提条件不会被继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看起来仍有一些合同未被继承的情况。在这种情况下,overriden方法失去了它的非null前提条件。
这个repro给出1个警告,但是不应该给出任何一个:
Looks like there are still some cases where contracts are not being inherited. In this case an overriden method loses its non-null precondition.
This repro gives 1 warning, but should give none:
<ContractClass(GetType(B(Of ).ContractClass))>
MustInherit Class B(Of T)
MustOverride Sub S(Of R As T)(ByVal arg As R)
<ContractClassFor(GetType(B(Of )))>
MustInherit Class ContractClass
Inherits B(Of T)
Overrides Sub S(Of R As T)(ByVal arg As R)
Contract.Requires(arg IsNot Nothing)
End Sub
End Class
End Class
Class C
Inherits B(Of IList(Of Byte))
Overrides Sub S(Of R As IList(Of Byte))(ByVal arg As R)
Contract.Assert(arg IsNot Nothing)
End Sub
End Class
推荐答案
请不要将契约类嵌套在另一个类中。目前,合同和相应的接口/抽象类应该是结构中的兄弟姐妹。我可以看出为什么它适用于非泛型类型,但它绝对是泛型的问题。
Please don't put the contract class nested inside another class. At the moment, the contract and corresponding interface/abstract class should be siblings in the structure. I can see why it works for non-generic types, but it is definitely a problem with generics.
这篇关于当方法具有泛型约束时,前提条件不会被继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文