I'm trying to write a contract for a constructor in VB.NET but it fails to cmpile with
Erreur 17 Malformed contract.: In method MyClass.#ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext), assembly [...]
The method body is
Protected 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?
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
Does the class have or inherit any events / members with withevents? Code contracts has a history of not getting along well with VB events and I do remember seeing 'malformed contract' in this sort of situation...
- Edited by Strilanc Wednesday, April 04, 2012 11:27 AM
This is still happening in the latest version of Code Contracts (1.4.60317.12) as detailed here: http://stackoverflow.com/questions/15817701/contract-requires-in-exception-ctor-fails-with-error-cc1027-malformed-contract
I have also been able to recreate the issue using version 1.4.50327.0 in VS2010 using the following code:
Imports System.Diagnostics.Contracts Public NotInheritable Class ValidationException Inherits Exception Public Property Result() As Object Public Sub New(ByVal ValidateResult As Object) Contract.Requires(Of ArgumentNullException)(ValidateResult IsNot Nothing, "ValidateResult is nothing.") Me.Result = ValidateResult End Sub End Class
- Edited by Mightymuke Tuesday, April 09, 2013 1:41 AM
I fixed this internally now.
The cause for this is some VB compiler inserted code that keeps track of objects passed to constructors. You might want to check out this and related blogs on the subject.
Cheers, -MaF (Manuel Fahndrich)