C# Dynamic Coding - Attributes in Depth
Last Updated :
28 Apr, 2025
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
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;
C#
using System;
namespace attributes {
class Program {
static void Main(string[] args)
{
Console.WriteLine("C# Dynamic code sample");
// Deprecated method call
TestMethod();
Console.ReadKey();
}
// "declaring the TestMethod() as Obsolete by attribute"
[Obsolete("Deprecated function", false)] public static void TestMethod()
{
Console.WriteLine("geeksForGeeks");
}
}
}
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 Custom and Predefined attributes in the source code. The forthcoming section depicts the implementation of some predefined attributes in the code.
Serialization
Let's have a look to the following class code having with the serialization attribute connotation with a non-serialized field as following;
C#
// Serialization class declaration with attribute
[Serializable] public class xyz {
public xyz() {}
string custName;
string Address;
// Non- serialized method attribute
[NonSerialized] int MobPhone;
}
We can again duly check both of the entry of serializing and non-serialized attributes entry into the IL code as following;

DllImport
The following code sample illustrates the functioning of [DllIport] attribute which calls an unmanaged dll user32.dll to populate a message box as following;
C#
public class xyz {
// Dll Import API call for MessageBox display
[DllImport("user32.dll", EntryPoint = "MessageBox")]
// MessageBox property configuration
public static extern int
ShowMessageBox(int hWnd, string text, string caption, uint type);
}
class Program {
static void Main(string[] args)
{
// Property value initialized
string caption = "geeksForGeeks";
string text = "[DLLImport] Attribute";
// Calling static method of xyz class
xyz.ShowMessageBox(0, text, caption, 0);
Console.ReadKey();
}
}
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;
C#
class Program {
static void Main(string[] args)
{
xyz obj = new xyz("GeeksforGeeks", "Hyderabad");
Console.WriteLine(obj.FullDetails());
Console.ReadKey();
}
}
public class xyz {
public xyz(string name, string country)
{
this.Name = name;
this.City = country;
}
public string FullDetails()
{
string str = Name + "-" + City;
return str;
}
private string Name;
private string City;
}
// Custom Attribute Class
[AttributeUsage(AttributeTargets.Class)] public class custAttribute : Attribute {
// Constructor
public custAttribute(string s)
{
this.CompanyName = s;
}
public string CompanyName
{
get;
set;
}
}
After compiling this aforesaid code, we can observe the custom attribute annotation happen in the metadata level by using ildasm.exe as follows;

Similar Reads
C# Tutorial
C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework
The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers
C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary
Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class
In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates
A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
C# .NET Framework (Basic Architecture and Component Stack)
C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
ASP.NET Interview Questions and Answer
ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# Data Types
Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays
An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read