microsoft / CodeContracts

Source code for the CodeContracts tools for .NET
Other
882 stars 150 forks source link

Contracts on Delegates #402

Open lundmikkel opened 8 years ago

lundmikkel commented 8 years ago

The Code Contracts User Manual from August 14, 2013 says:

Currently, there is no mechanism to provide contracts on delegate types or delegate values. In the future, we will support a mechanism similar to contracts for interfaces that will allow associating requires and ensures to delegate types.

To the best of my knowledge, this still isn't possible. Are there any plans to add this?

Likewise, it would be beneficial to allow contracts on events. For instance, ensuring that a method raises an event, and that the values in the event handler are as expected.

SergeyTeplyakov commented 8 years ago

This is an interesting request. The short answer on first question: this still impossible.

But I would like to clarify what exactly you meant by 'contracts for delegates'?

Do you want to define a contract on the delegate type? Like this:

// Want to define that s is not null and result is not null?
public delegate string Foo(string s);

I assume that this sceanrio was mentioned in Code Contracts User Manual and the reason why this is complicated to have is because there is no way to express contracts (need another attirbute ContractForDelegate?) and not clear how to enforce them at runtime (I'm not even talking about static checker).