Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to have an empty attribute class?

I have created an attribute which will define which properties have "special" data. My Attribute looks something like this:

[AttributeUsage(AttributeTargets.Property)]
    public class TestAttribute : Attribute
    {
    }

I don't need anything in the body of the class because all it's doing is identifying the properties. Is this bad practice? If so, what are better alternatives?

like image 645
CodingMadeEasy Avatar asked Oct 15 '25 15:10

CodingMadeEasy


1 Answers

No. Attributes are frequently used just as flags.

Take the pervasive:

[Serializable]

If you reflect into its source code, it's basically blank inside (it just has some constructors), but it's one of the most used attributes in .NET.

The same thing goes for attributes like [XmlIgnore].

like image 93
Colin Avatar answered Oct 18 '25 04:10

Colin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!