[英]How to send an error message to the Debugger and stopping the debug?
I want to check some conditions in my code and if the condition not satisfied I want show an error on the Debug when the VS(C#) errors shown, 我想检查我的代码中的一些条件,如果条件不满意,我希望在显示VS(C#)错误时在Debug上显示错误,
I tried to do that with Debug.Assert()
but it didn't work like what I expected. 我尝试用
Debug.Assert()
做到这一点,但它没有像我期望的那样工作。 this is my code: 这是我的代码:
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;
}
}
when I use this attribute without any arguments my project build successfully 当我使用此属性而没有任何参数时,我的项目成功构建
// ...
[Inflicted]
public string Title
{
get { return _Title; }
set { _Title = value;}
}
// ...
but I want the user of this attribute can't use my attribute only with arguments. 但是我希望这个属性的用户不能仅使用我的属性和参数。 looks like:
[Inflicted("param1", "param2")]
看起来像:
[Inflicted("param1", "param2")]
If you want a compile error, Debug.Assert
is not for you: it creates errors when running the application in debug. 如果您想要编译错误,
Debug.Assert
不适合您:它在调试中运行应用程序时会产生错误。
To create an error on compilation, you should change your constructor: 要在编译时创建错误,您应该更改构造函数:
public Inflicted (string param1, params string[] otherParams) {
}
Since the params
argument can be empty, this will force calls to the constructor to have at least 1 argument. 由于
params
参数可以为空,这将强制调用构造函数至少有一个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.