Well, simply saying, it is accepted, because the compiler was written in such way.
There's a requirement that all Attribute
s must derive from the Attribute
class. Therefore it's can be assumed that everything in brackets []
is an 'Attribute'.
There's a programmers' custom that things XXXX are often being named XxxxThing, PersonView, LoginController, ReadOnlyAttribute. It gives you a bit better look on the code when you are looking at files. However, when you deal only with controllers, only with views, or only with attributes, it's tiresome to have to always say XxxxAttribute, YyyyAttribute.
Since you know that all things written in []
are of the Attribute
class, it's not really necessary to write the Attribute
word contained in the class name. It adds nothing. You know it's an attribute because it's in the []
.
It's just a handy shortcut provided by the language/compiler, for your convenience only, and only available in this place in the code. If you search with Reflection for that attribute classes, or if you try to Activator.Create<>
or even typeof()
it, you still need the full name.
And about the problem with custom attribute:
- make sure you've got your
using
s right
- make sure you have added the reference
- make sure you have recompiled all assemblies
- ensure that
typeof(JeffthrotizeAttribute)
does not complain aobut unknown type. If so - get to the first three points
- if the compiler still complains about
[Jeffthrotize]
than most probably you've got the flags wrong. Remember that attributes need to have AttributeUsage
specified correctly. If you specify AttributeTargets.Method
, then you will not be able to put that attribute on a whole class. You can combine the attribute-usages with |
operator, like any flag: AttributeTargets.Method|AttributeTargets.Class
, etc.
See http://msdn.microsoft.com/pl-pl/library/system.attributetargets.aspx
for full list.