How to define code contracts, the delegate specified as an argument is Pure?

Consider the following code:

int SomeField;
void Foo([Pure] Func<int, object> getData)
{
    Contract.Requires(getData != null);
    Contract.Requires(getData(this.SomeField) != null);
}

I get the following warning:

The method call ' System.Func'2<System.Int32,System.Object>.Invoke(System.Int32)' without [Pure]in the contracts method '.... Foo(System.Func'2<System.Int32,System.Object>)' was detected

This warning makes sense. But I would still like to call the delegate in the contracts and not get a warning (suppose the warnings were turned into errors). How do I achieve this?

I tried the attribute Pureas shown in the example, but this does not work.

I would also like to know why a parameter PureAttributecan be specified in parameters. It does not make sense if the parameter type is not a delegate type, and even if it is, it does not work, as I expected, as I said above.

+5
2

Code Contracts - , :

[Pure]
public delegate U PureFunc<in T, out U>(T thing);

, , , , :)

+1

, , , , . , , , .

+1

All Articles