[英]How to send an error message to the Debugger and stopping the debug?
我想檢查我的代碼中的一些條件,如果條件不滿意,我希望在顯示VS(C#)錯誤時在Debug上顯示錯誤,
我嘗試用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")]
如果您想要編譯錯誤, Debug.Assert
不適合您:它在調試中運行應用程序時會產生錯誤。
要在編譯時創建錯誤,您應該更改構造函數:
public Inflicted (string param1, params string[] otherParams) {
}
由於params
參數可以為空,這將強制調用構造函數至少有一個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.