Attributes in ASP.Net C#

Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time using a technique called Reflection.

Attributes exist in two forms: attributes that are defined in the Common Language Runtime's base class library and custom attributes that you can create, to add extra information to your code. This information can later be retrieved programmatically.

In this example, the attribute System.Reflection.TypeAttributes.Serializable is used to apply a specific characteristic to a class:

[System.Serializable]
public class SampleClass
{
// Objects of this type can be serialized.
}

Attributes have the following properties:
  • Attributes add metadata to your program. Metadata is information embedded in your program such as compiler instructions or descriptions of data.
  • Your program can examine its own metadata using Reflection.
  • Attributes are commonly used when interacting with COM.
Tags: , ,
Hot on Web:


About author