I have just got started using Microsoft's Code Contracts and already ran into a problem that I can't solve. Let's take this interface for which I'd like to specify a contract:
public interface IRandomWriteAccessible<T>
{
T this[uint index] { set; }
uint Length { get; }
}
The documentation says to use the ContractClass
attribute when specifying a contract for an interface. However, the compiler will complain about this:
[ContractClass(typeof(IRandomWriteAccessibleContract<T>))]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- compiler error
public interface IRandomWriteAccessible<T>
{
...
}
[ContractClassFor(typeof(IRandomWriteAccessible<T>))]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- compiler error
public sealed class IRandomWriteAccessibleContract<T> : IRandomWriteAccessible<T>
{
...
}
It seems that type parameters cannot be used for attributes.
How do I write a contract for my interface, or is this not possible with Code Contracts?