How to handle when an attribute is invalid?

I have an attribute. Let me call him SomeAttribute . Ideally, it will be defined as follows:

 public class SomeAttribute : Attribute { public MyType[] MyObjects { get; set; } public SomeAttribute(params MyType[] parameters) { MyObject = parameters } } 

However, due to attribute restrictions, it is defined as follows:

 public class SomeAttribute : Attribute { public MyType[] MyObjects { get; set; } public SomeAttribute(params string[] parameters) { MyObject = parameters.DoConvert(); //basically convert them to a MyType[] } } 

However, if the strings are invalid, I thought I should throw an exception. But I thought that "the attributes are special, I think there should be something else in the exceptions." Then I found the following: http://lostechies.com/jimmybogard/2008/11/23/beware-exceptions-in-attribute-constructors/ . Essentially, it says that β€œthrowing exceptions in attribute constructors is not a good idea.”

So what should I do here? Should I make an exception? Should I add a bool that returns true if the attribute is invalid?

+4
source share

Source: https://habr.com/ru/post/1483786/


All Articles