Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to stop release compilation with error

Tags:

c#

As you're developing you often use things like

throw new NotImplementedException("Finish this off later") 

or

// TODO - Finish this off later

as a placeholder to remind you to finish something off - but these can be missed and mistakenly end up in the release.

You could use something like

#if RELEASE
   Finish this off later
#endif

so it won't compile in Release build - but is there a more elegant way?

like image 923
Ryan Avatar asked Feb 06 '12 15:02

Ryan


2 Answers

I saw an elegant implementation here

#if DEBUG

namespace FakeItEasy
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// An exception that can be thrown before a member has been
    /// implemented, will cause the build to fail when not built in 
    /// debug mode.
    /// </summary>
    [Serializable]
    [SuppressMessage("Microsoft.Design",   
        "CA1032:ImplementStandardExceptionConstructors", 
        Justification = "Never used in production.")]
    public class MustBeImplementedException
        : Exception
    {
    }
}

#endif
like image 103
PHeiberg Avatar answered Oct 21 '22 13:10

PHeiberg


I would recommend to use #warning:

#warning Finish this off later

And in the Release configuration set Treat Warnings as Errors to True.

In this case in Debug you will see it only as warnings but in release it will throw exceptions.

like image 24
Samich Avatar answered Oct 21 '22 13:10

Samich