Always will be ready notify the world about expectations as easy as possible: job change page
Jun 29, 2023

Attributes and Decorators in C#

Megha Prasad

Attributes and Decorators in C#

Attributes and decorators, which allow you to attach metadata to classes, properties, and methods.


Attributes in C# allow you to attach metadata to your classes, methods, and other code elements. This metadata can then be used at runtime to modify the behavior of your code. Attributes are applied to code elements using square brackets, and they can contain additional information such as parameters.

For example, you can use the [Serializable] attribute to indicate that an object can be serialized and saved to disk. The [Obsolete] attribute can be used to indicate that a method or class should not be used anymore. You can also create your own custom attributes to add information to your code.

To create a custom attribute in C#, you need to create a class that derives from the System.Attribute class. This class can contain properties and constructor parameters that allow you to pass additional information to the attribute.

An attribute is a class that derives from the System.Attribute base class and is used to store metadata about a class, property, or method. Attributes are typically used to provide information about the target, such as its intended use, permissions required, or other relevant details. For example, the Obsolete attribute is used to mark a class, property, or method as deprecated and inform the compiler that it should generate a warning if the deprecated item is used.

Here’s an example of a custom attribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class MyAttribute : Attribute
    public string Message { get; set; }
    public MyAttribute(string message)
        Message = message;

In this example, the MyAttribute class contains a property Message and a constructor that takes a string argument. The AttributeUsage attribute specifies the targets that the attribute can be applied to, in this case, classes and methods. The AllowMultiple property allows the attribute to be applied multiple times to the same code element.

To use the attribute, simply apply it to a class or method using square brackets:

[MyAttribute("Hello World")]
public class MyClass

You can also retrieve the values of attributes at runtime using reflection. Reflection is a feature in C# that allows you to inspect and manipulate the metadata of types at runtime. With reflection, you can inspect the properties, methods, and events of a type, and even create and execute code dynamically. To retrieve the values of attributes, you can use the GetCustomAttributes method of the Type class.


Decorators are a feature in C# that provide a way to add additional behavior to existing classes and methods. Unlike attributes, which are applied using square brackets, decorators are applied using the decorator keyword. Decorators allow you to wrap existing code elements with additional functionality, without modifying the original code.

The decorator class must have a reference to the code element it decorates and can modify its behavior in various ways, such as adding pre- and post-processing logic, logging, caching, and more.

For example, you can create a decorator to log the execution time of a method. Here’s an example of a decorator in C#:

public class Logger
    public void LogBefore(string message)
        Console.WriteLine($"Logging before execution: {message}");

    public void LogAfter(string message)
        Console.WriteLine($"Logging after execution: {message}");

public class LoggingDecorator
    private Logger _logger;

    public LoggingDecorator(Logger logger)
        _logger = logger;

    public void Decorate(Action action)
        _logger.LogBefore("Starting method execution");
        _logger.LogAfter("Finished method execution");

In this example, the Logger class contains two methods for logging messages. The LoggingDecorator class takes a reference to a Logger object in its constructor and implements the Decorate method, which takes an Action as an argument. The Decorate method logs a message before and after the execution of the action.

To use the decorator, you can pass a method to the Decorate method, like this:

var logger = new Logger();
var decorator = new LoggingDecorator(logger);

decorator.Decorate(() => Console.WriteLine("Hello World"));

This will log a message before and after the execution of the method, like this:

Logging before execution: Starting method execution
Hello World
Logging after execution: Finished method execution

Decorators can be very powerful, especially when used in combination with other language features such as generics and delegates. They allow you to add behavior to your code in a modular and composable way, making it easier to maintain and extend over time.

Jul 24, 2023
Author: Mario Bittencourt
We have been living in a distributed world for quite some time, and when considering how should you implement the communication between any two parties involved, the discussion seems to gravitate around two options: API or messaging.Instead of focusing on...
Oct 21, 2023
Also known as Pseudo-Analysis and Blind Development, Mushroom Management is often described by this phrase: "Keep your developers in the dark and feed them fertilizer."An experienced system architect recently stated, "Never let software developers talk to end users." Furthermore, without...
Jul 25, 2023
Author: Anthony Trad
Bending the Clean Architecture PrinciplesAsync await memeIntroductionImagine you’re a chef in a kitchen full of ingredients, some fresh, some a bit past their prime, all thanks to Microsoft’s “We never throw anything away” policy. This is what programming asynchronously in...
Oct 21, 2023
AntiPattern ProblemA Dead End is reached by modifying a reusable component, if the modified component is no longer maintained and supported by the supplier. When these modifications are made, the support burden transfers to the application system developers and maintainers....
Написать сообщение

© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban