Code contracts in VB.Net Constructors

提議的解答 Code contracts in VB.Net Constructors

  • Monday, April 02, 2012 5:51 PM
     
      Has Code

    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 Sub
    Where 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

All Replies

  • Tuesday, April 03, 2012 9:53 PM
    Owner
     
      Has Code

    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
    

    Mike Barnett

  • Wednesday, April 04, 2012 11:27 AM
     
     

    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
    •  
  • Tuesday, April 09, 2013 1:39 AM
     
      Has Code

    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
    •  
  • Wednesday, April 10, 2013 4:06 PM
    Owner
     
     

    Thanks, I can repro it.


    Cheers, -MaF (Manuel Fahndrich)

  • Wednesday, April 10, 2013 6:59 PM
    Owner
     
     Proposed Answer

    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)


  • Wednesday, April 10, 2013 9:16 PM
     
     

    Thanks for the fast resposne.

    I'm hoping that the fix isn't included in the version that's just been released (1.5.60409.11) as I've just upgraded and am still getting the error.

    MM

  • Wednesday, April 10, 2013 10:18 PM
    Owner
     
     

    No, the fix has not been released yet. So what you are seeing is consistent.


    Cheers, -MaF (Manuel Fahndrich)