2012-02-06 95 views
15

當你開發你經常使用的東西,像優雅的方式來停止與錯誤版本編譯

throw new NotImplementedException("Finish this off later") 

// TODO - Finish this off later 

作爲佔位符來提醒你完成的東西 - 不過這些都可以被錯過並錯誤地在發佈中結束。

您可以使用類似

#if RELEASE 
    Finish this off later 
#endif 

,所以它不會在發佈版編譯 - 但有一個更優雅的方式?

+0

創建技術債務任務(JIRA,TFS,CQ,無論)? – sll 2012-02-06 15:08:29

+0

順便說一句,我會爭辯說,這樣的檢查應該是構建過程的一部分,也許檢查?大多數版本控制系統都有可以處理這種情況的鉤子。 – Snowbear 2012-02-06 15:09:57

+1

@Snowbear所以你不要讓他們檢查,直到一切完成 - 或者你只是不建立一個TODO? – weston 2012-02-06 15:13:20

回答

11

我看到了一個優雅的實現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 
+0

它是如何工作的 – crazy2be 2012-02-06 20:51:44

+2

@ crazy2be - 你讓你未完成的方法拋出'MustBeImplementedException'編譯器指令'#if DEBUG'確保'MustBeImplementedException'類只能編譯in調試,所以對於發佈版本,任何被遺忘的throw都會導致構建中斷,因爲該類不可用。 – PHeiberg 2012-02-06 21:03:53

10

我會建議使用#warning

#warning Finish this off later 

而且在Release配置設置Treat Warnings as ErrorsTrue

在這種情況下,您將在Debug中看到它僅作爲警告,但在發佈時它會拋出異常。

+0

問題是我們想要忽略一些警告(例如調用現在在新版本中標記爲[廢棄]的API方法,但我們希望新舊版本的單一來源的方法 – Ryan 2012-02-06 15:20:03

+1

您可以配置應忽略哪些類型的警告。是相關的線程:http://stackoverflow.com/questions/267168/treat-all-warnings-as-errors-except-in-visual-studio – Samich 2012-02-06 15:32:46

1

您可以封裝此在它自己的方法中,這樣它只需要在一個地方換一個相關的構建。

void NotImplemented() 
{ 
    #if RELEASE 
     Finish this off later 
    #endif 
}