Question

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,
I tried to do that with Debug.Assert() but it didn't work like what I expected. 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")]

Was it helpful?

Solution

If you want a compile error, Debug.Assert is not for you: it creates errors when running the application in debug.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top