C# Dynamic Coding – Attributes in Depth
The .NET common language offers the implementation of dynamic coding practice at runtime by introducing the Attributes that enable to associate custom metadata which are generated compile-time and embedded in the .NET assembly with program elements. Furthermore, these metadata can easily be examined by employing the .NET mechanism called Reflection. So, this article bestows a brief understanding of the usage of the .NET attributes in the dynamic programming perspective.
Attributes derived from the System.Attribute class. Attributes can be positioned on most any revelation, however, a particular characteristic may limit the kinds of presentations on which it is substantial. In .NET code, we can determine the attributes by putting the name of the attributes encased in square sections () over the declaration of the element to which it applies. Besides, we can incorporate extra metadata into a get together utilizing qualities. More so, attributes resemble descriptive words, utilized for metadata explanation like COM components that can be applied to a given kind, get together, modules, techniques, and many more. Here is the syntax of an attribute in .NET programming as following;
[type: attributeName(parameter1, parameter2, ………n)]
The .NET constructs specify two types of properties for the usage of attributes either as Predefined Attributes or Custom Attributes. Attributes typically can have either zero or more parameters. The following C# code is, therefore, poses the execution of Attributes where a method is being declared as deprecated using obsolete as;
The ILDASM utility of the .NET framework could be harness to verify the entry of the Obsolete attribute in the generated MSIL code of the corresponding aforesaid code through as following;
The ILDASM utility generates the code behind IL code to a .NET assembly, hence, double-click on the obsolete method myFun() entry, and it reflects the entry of the Obsolete attribute as following;
Attributes serve the purpose of information definition, reflection, web services, serialization, set-up the class blueprint, and specify the third party library at run-time. Overall, it is conducive to make an entry to the metadata table for a documentation point of view. Moreover, the compile automatically discovers the presence of the attribute entry during debugging. The .NET framework stipulates Custome and Predefined attributes in the source code. The forthcoming section depicts the implementation of some predefined attributes in the code.
Let’s have a look to the following class code having with the serialization attribute connotation with a non-serialized field as following;
We can again duly check both of the entry of serializing and non-serialized attributes entry into the IL code as following;
The following code sample illustrates the functioning of [DllIport] attribute which calls an unmanaged dll user32.dll to populate a message box as following;
Up till now, we have experienced the pre-defined attributes, in this series, we can further develop our own custom attributes too, and leverage it in other code. For this, the class must be prefixed with attributes and derived to System. Attribute class as following;
After compiling this aforesaid code, we can observe the custom attribute annotation happen in the metadata level by using ildasm.exe as follows;