Building with Code Contracts?

前端 未结 2 2024
我在风中等你
我在风中等你 2020-12-15 20:36

I have the following method:

private void DoSomething(CoolClass coolClass)
{
    if (coolClass == null)
    {
        throw new ArgumentNullException(\"coolC         


        
相关标签:
2条回答
  • 2020-12-15 21:04

    By changing following project properties I could eliminate getting this exception while running.

    Right click on project -> Properties -> Code Contract (Tab) change the assembley mode to "Standard Contract Requires" also select checkbox - Perform Runtime contract checking

    0 讨论(0)
  • 2020-12-15 21:07

    Why don't you just write your own version of the method, if you like the simplicity?

    public class CustomContract
    {
        public static void Requires<TException>( bool Predicate, string Message )
            where TException : Exception, new()
        {
           if ( !Predicate )
           {
              Debug.WriteLine( Message );
              throw new TException();
           }
        }
    }  
    

    Using Code Contracts just to have a friendly API sounds like shooting sparrows with a cannon.

    0 讨论(0)
提交回复
热议问题