C# Code Contracts with Builder Pattern - "Possibly calling a method on a null reference" -


i'm investigating code contracts , i'm implementing builder pattern this:

public class personcaution {     private personcaution()     {     }      public string cautiontype { get; private set; }     public string remarks { get; private set; }      public class builder     {         private string _cautiontype;         private string _remarks;          public builder withcautiontype(string value)         {             contract.ensures(contract.result<builder>() != null);              _cautiontype = value;                             return this;         }          public builder withremarks(string value)         {             contract.ensures(contract.result<builder>() != null);              _remarks = value;             return this;         }          public personcaution build()         {             contract.ensures(contract.result<personcaution>() != null);              return new personcaution             {                 cautiontype = _cautiontype,                 remarks = _remarks             };         }     } } 

here's snippet showing how use builder class:

if (row != null) {     var builder = new personcaution.builder()         .withcautiontype((string)row.element("personcaution__type1g"))         .withremarks((string)row.element("personcaution__remarks"));      if (builder != null)     {         personcautions.add(builder.build());     } } 

however, code contracts static checker fails error:

possibly calling method on null reference. expect nwp.pointservices.domain.model.people.personcaution+builder.withcautiontype(system.string) returns non-null?

q. thought contract.ensures postcondition satisfy static checker, doesn't. need remove error? v much.

note. see issue if builder class in separate project code calls it.

more info:

  • visual studio professional 2015 14.0.25424.00 update 3
  • all projects target .net 4.6.1
  • code contracts installed via visual studio extension, v 1.8
  • i'm using code contracts in other (non builder) areas of project

as have found out, need enable build contract references cc project tab enable cross-project analysis ("contract reference assembly" = "build")


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -