Attributes & Reflection

Attributes are a simple technique for adding metadata information and behavior to code within applications. Reflection technique can be used to pick up the attribute related information at runtime. An object is described by the values of its attributes. A car can be described by its make, model, and color. Similarly c sharp has certain attributes which depict and influence its behavior.


An attribute is declarative tag that is used to convey information to runtime about the behavior of programmatic elements such as classes, assemblies.
There are various predefined attributes in .Net. You can create your own attribute by using System.Attribute namespace.

The following syntax enables you to specify an attribute:

                    [attribute (positional_parameters, name_parameter=value, ….)]



Reflection is used in the process of obtaining type information at runtime.
The classes that give access to the metadata of a running program are in
System.Reflection namespace.
Reflection provides objects that encapsulate assemblies, modules, and types.

// dynamically load assembly from file Test.dll

Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");

// get type of class Calculator from just loaded assembly

Type calcType = testAssembly.GetType("Test.Calculator");


Leave Comment