Follow the .Net convention of appending Attribute to the name of your Attribute class at your own risk.

I have been delving more and more into custom attribute programming more and more lately and the following is a quote from my validation article (click here to view the article).

It is a .NET convention to append “Attribute” to the name of each custom attribute type class. Then when assigning an attribute to an item, you may leave off the “Attribute” portion of the name so that you can decorate your items in the following manner [RequiredItem()] or [RegEx(….)]. This feature has a side effect. If you define two attribute type classes, one with the name RegExAttribute and the other RegEx you will be able to compile your project; however, when you attempt to use the class RegEx, you will get the following compilation error:

’RegEx’ is ambiguous; use either ‘@RegEx’ or ‘RegExAttribute’

I recommend you follow the .NET convention; regardless, it is a good idea to pick a single convention and stick with it.

2 Comments

  • Always good to know, but I would argue that this would probably only bite you in a scenario where the name of one of two class was chosen poorly. You mention you're working on validation, so I would imagine that your attribute should be called RegexValidationAttribute as opposed to just RegexAttribute. If it was just RegexAttribute I wouldn't even have a clue as to what it does just by looking at the class name.



    Just my 2¢,

    Drew

  • I actually did call it RegExAttribute but it was in the Cybral.Validation namespace...if that helps ;)



    -Mathew NOlton

Comments have been disabled for this content.