0

I am fairly new to Code Contracts...and I ran into a problem.

I have in a method LINQ query that go something like this:

MyClass[] fields =
            (from p in rType.GetProperties()
             where p.CanRead
             let fAttr = p.GetCustomAttributes(typeof(MyClassAttribute), true).SingleOrDefault() as MyClassAttribute
             where fAttr != null
             select new MyClass(p, fAttr)).ToArray();

And I want to implement Code Contracts in my project. I have done everything OK, until I got to this point. When I run static checker, it suggest to me that I need to add a couple of preconditions (Contract.Requires) regarding variables p and fAttr which are defined in the query. And also, I have a couple of unproven requires.

How can I solve this? Any ideas?

MyClass also contains two preconditions:

internal MyClass(PropertyInfo p, MyClassAttribute att)
    {
        Contract.Requires(p != null);
        Contract.Requires(att != null);
        ...
    }

Thanks in advance :)

skaffman
  • 398,947
  • 96
  • 818
  • 769
Behemoth
  • 11
  • 4

1 Answers1

0

I can't seem to reproduce this. Are you using the latest version of Code Contracts?

My entire code looks like this... is this close enough to your version?

using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;

namespace ConsoleApplication10
{
    class Program
    {
        class MyClassAttribute : Attribute{}
        class MyClass
        {
            internal MyClass(PropertyInfo p, MyClassAttribute a)
            {
                Contract.Requires(p != null);
                Contract.Requires(a != null);
            }
        }

        static void Main(string[] args)
        {
            var rType = typeof (DateTime);

            MyClass[] result = (from p in rType.GetProperties()
             where p.CanRead
             let fAttr = p.GetCustomAttributes(typeof(MyClassAttribute), true).SingleOrDefault() as MyClassAttribute
             where fAttr != null
             select new MyClass(p, fAttr)).ToArray();

        }
    }
}
porges
  • 30,133
  • 4
  • 83
  • 114
  • 1
    Oh dear, just realised I had my StackOverflow filtered to "unanswered" questions, have been answering old questions for a while now... :P – porges Jan 19 '11 at 10:20