4

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?

1
  • i think its perfectly fine.
    – George
    May 28, 2015 at 21:31

2 Answers 2

9

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

2

Colin points out a good use.

And there are many others:

[PrimaryKey]
[AutoIncrement]

Essentially, they can be whatever you want them to be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.