Proposed Answer Contracts and reflection

  • Wednesday, November 11, 2009 2:22 PM
     
      Has Code
    When I verify contracts on this code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Diagnostics.Contracts;
    
    namespace ContractTest
    {
        public sealed class MyClass
        {
            public int MyProperty { get; set; }
    
            public void Test()
            {
                Contract.Requires(GetType() != null);
                Contract.Requires(GetType().GetProperties() != null );
                Contract.Requires(GetType().GetProperties().Length == 1);
            }
    
            public void Test2()
            {
                Test();
            }
        }
    }
    

    I get

    MyClass.cs(23,13): warning : CodeContracts: requires unproven
    MyClass.cs(18,13): warning :   + location related to previous warning
    It's complaining about the line
    Contract.Requires(GetType().GetProperties().Length == 1);
    
    Am I missing something or is the static checker unable to verify constraints based on reflection?

All Replies

  • Wednesday, December 02, 2009 7:37 PM
    Owner
     
     Proposed Answer
    Hello,
      yes, it's correct, at the moment the static checker does not use reflection to validate contracts.

    ciao,
    f