繁体   English   中英

如何向调试器发送错误消息并停止调试?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM