JREAM
JREAM

Reputation: 5941

C# Function Prefix Attribute Names?

In C# what are those brackets called above a method in MVC 3?

[ErrorHandler, SomethingHere]
public function Test() {


}

Upvotes: 0

Views: 499

Answers (5)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28588

These are called Attributes.An attribute is a class that inherits from the abstract class System.Attribute. By convention, all attributes are given a class name that ends with the word “Attribute”. Here are some MVC3 Attributes:

   AcceptViewAttribute
   ActionFilterAttribute
   ActionMethodSelectorAttribute
   ActionNameAttribute
   ActionNameSelectorAttribute
   AuthorizeAttribute
   BindAttribute
   CustomModelBinderAttribute
   FilterAttribute
   HandleErrorAttribute
   HiddenInputAttribute
   HttpDeleteAttribute
   HttpGetAttribute
   HttpPostAttribute
   HttpPutAttribute
   ModelBinderAttribute
   NonActionAttribute
   OutputCacheAttribute
   RequireHttpsAttribute
   ValidateAntiForgeryTokenAttribute
   ValidateInputAttribute

and you can create your Custom Attributes

Upvotes: 3

Will
Will

Reputation: 3670

Not sure what you mean by "those brackets". What is preceding the function is an Attribute.

Microsoft MSDN: System.Attribute

And to expand a little in regards to usage:

An attribute is an annotation that can be placed on an element of source code and used to store application-specific information at compile time. This information is stored in the metadata and can be accessed either during application execution, through a process known as reflection, or when another tool reads the metadata. Attributes might change the behavior of the application during execution, provide transaction information about an object, or convey organizational information to a designer. gnu.org

Upvotes: 5

THBBFT
THBBFT

Reputation: 1211

Attributes

It infers the word Attribute, so your example is synonymous with:

[ErrorHandlerAttribute, SomethingHereAttribute]
public function Test() {

Upvotes: 2

Ivan Manzhos
Ivan Manzhos

Reputation: 743

The MVC runtime uses Reflection to find attributes. Then MVC uses this information about located attributes to find the way how the method will be executed, what are the security restrictions and so on

Upvotes: 2

Mike Corcoran
Mike Corcoran

Reputation: 14564

those are called method attributes. you can read more on the msdn site

Upvotes: 1

Related Questions