本文介绍了VB.Net构造函数中的代码契约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试在VB.NET中为构造函数编写一个合同,但它无法使用 Erreur 17 格式错误的合同:在方法MyClass中。 #ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext),assembly [...] 方法体是 受保护的 Sub 新(info As SerializationInfo , context As StreamingContext ) MyBase 。新(info, context)合约。合约。确保(MediaType IsNot Nothing )合约s。合同 .Ensures(MissingProperties IsNot 没有)合同。合同 .Ensures(MissingProperties.Count > 0) _MediaType  ; = info.GetValue(" MediaType" , GetType (PhysicalObjects。 MediaType )) _MissingProperties = info.GetValue(" MissingProperties" ,  ; GetType ( ICollection ( ; 字符串))) 结束 Sub 其中MediaType和MissingProperties属于成员属性。我认为问题与合同不是第一条指令但MyBase.New也需要成为第一条指令这一事实有关。然后,任何人都有解决方案吗? BKQc 解决方案 我恐怕不是能够重现这个问题。你能发一个完整的例子吗?例如,这是我尝试的示例,它似乎工作正常。 Imports System.Diagnostics.Contracts 公共类B 继承System.Object Public Sub New(a as Integer) End Sub End Class Public Class C 继承B 受保护的子新(x为整数) MyBase.New(x) Contract.Requires(0< x) Contract.Ensures(Name IsNot Nothing) End Sub 公共属性名称字符串结束类 I'm trying to write a contract for a constructor in VB.NET but it fails to cmpile withErreur 17 Malformed contract.: In method MyClass.#ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext), assembly [...]The method body isProtected Sub New(info As SerializationInfo, context As StreamingContext)MyBase.New(info, context)Contracts.Contract.Ensures(MediaType IsNot Nothing)Contracts.Contract.Ensures(MissingProperties IsNot Nothing)Contracts.Contract.Ensures(MissingProperties.Count > 0)_MediaType = info.GetValue("MediaType", GetType(PhysicalObjects.MediaType))_MissingProperties = info.GetValue("MissingProperties", GetType(ICollection(Of String)))End SubWhere MediaType and MissingProperties are to member properties. I suppose the problem has something to do with the fact that contracts are not the first instruction but MyBase.New needs also to be the first instruction. Then, anyone has a solution?BKQc 解决方案 I'm afraid I was not able to reproduce the problem. Can you please post a complete example? For instance, this is the example I tried and it seemed to work just fine.Imports System.Diagnostics.Contracts Public Class B Inherits System.Object Public Sub New(a as Integer) End Sub End Class Public Class C Inherits B Protected Sub New(x as Integer) MyBase.New(x) Contract.Requires(0 < x) Contract.Ensures(Name IsNot Nothing) End Sub Public Property Name As String End Class 这篇关于VB.Net构造函数中的代码契约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 06:32