I have a problem with CodeContracts. It does not rewrite condition in my method. Only in one method while it works in all other class methods. This strange method generates the Assert in Run-Time, in spite of my settings.
But when i rewrote this code with Linq its work correct.
I use .Net 3.5 and CodeContracts 1.4.30707.2
Part of my class:
internal static class MetadataHelper
{
// Does not work Generates Assert, in spite of my settings
internal static PropertyInfo[] RemoveDuplicateProperties(PropertyInfo[] candidates)
{
Contract.Requires(candidates != null);
var result = new List<PropertyInfo>(candidates);
foreach (var q1 in candidates)
{
if (Array.Exists(
candidates, q2 =>
(
q1.Name == q2.Name &&
GetHierarchyLevel(q1.DeclaringType) < GetHierarchyLevel(q2.DeclaringType)
)))
{
result.Remove(q1);
}
}
return result.ToArray();
}
// And when i rewrite it with linq it works
internal static PropertyInfo[] DoSomething(PropertyInfo[] ccs)
{
Contract.Requires(ccs != null);
return ccs.Where(q1 =>
!ccs.Any(q2 =>
(
q1.Name == q2.Name &&
GetHierarchyLevel(q1.DeclaringType) < GetHierarchyLevel(q2.DeclaringType)
))).ToArray();
}
}