2014-05-01 49 views
0

我要檢查一些條件在我的代碼,如果條件不滿足我要顯示在調試錯誤時,VS(C#)錯誤顯示,
我試圖做到這一點與Debug.Assert(),但它並沒有像工作我所期望的。 這是我的代碼:如何向調試器發送錯誤消息並停止調試?

public class Inflicted : Attribute 
    { 
     public string[] Names{ get; set; } 
     public Inflicted (params string[] Names) { 

      // check if the params empty; 
      // so show the error in debuger. 
      Debug.Assert(Names.Length == 0, "The Inflicted cant be with zero argument"); 

      this.Names= Names; 
     } 
} 

當我使用這個屬性不帶任何參數我的項目構建成功

// ... 
[Inflicted] 
public string Title 
{ 
    get { return _Title; } 
    set { _Title = value;} 
} 
// ... 

但我想這個屬性的用戶不能使用我的屬性只與爭論。 看起來像:[Inflicted("param1", "param2")]

+0

您是否期望在調試期間或編譯期間發生錯誤? –

+0

@WillemDuncan是的,我想導致編譯錯誤。 –

回答

2

如果你想編譯錯誤,Debug.Assert不適合你:它在調試中運行應用程序時會產生錯誤。

要創建的編譯錯誤,你應該改變你的構造函數:

public Inflicted (string param1, params string[] otherParams) { 

} 

由於params參數可以是空的,這將迫使調用構造函數中至少有1說法。

相關問題