c-sharp
  1. c-sharp-c-custom-attribute

C# Custom Attributes

In C#, attributes are a way to add metadata to your code. Custom attributes allow you to define your own metadata that can be attached to types, methods, properties, and other elements in your code. In this tutorial, we'll discuss how to create and use custom attributes in C#.

Syntax

The syntax for creating a custom attribute in C# is as follows:

[AttributeUsage(AttributeTargets.Element)]
public class MyCustomAttribute : Attribute {
   // fields, properties, methods, etc.
}

This defines a custom attribute called "MyCustomAttribute" that can be attached to any element in your code (specified by the AttributeTargets enum). You can add fields, properties, and methods to your custom attribute as needed.

Example

Let's say we want to create a custom attribute that marks a method as deprecated. Here's how we can implement it:

[AttributeUsage(AttributeTargets.Method)]
public class DeprecatedAttribute : Attribute {
   public string Message { get; set; }

   public DeprecatedAttribute(string message) {
      Message = message;
   }
}

public class MyClass {
   [Deprecated("This method is deprecated.")]
   public void MyMethod() {
      Console.WriteLine("MyMethod called.");
   }
}

Now, we can use the custom attribute in our code:

MyClass myClass = new MyClass();
myClass.MyMethod(); // Output: "MyMethod called."

MethodInfo methodInfo = typeof(MyClass).GetMethod("MyMethod");
DeprecatedAttribute attribute = (DeprecatedAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(DeprecatedAttribute));
Console.WriteLine(attribute.Message); // Output: "This method is deprecated."

Output

When we run the example code above, the output will be:

MyMethod called.
This method is deprecated.

This is because we called the "MyMethod" method on an instance of the "MyClass" class, and then retrieved the "DeprecatedAttribute" custom attribute attached to the method using reflection.

Explanation

In the example above, we created a custom attribute called "DeprecatedAttribute" that can be attached to methods. We added a "Message" property to the attribute to provide a message indicating why the method is deprecated.

We then attached the "DeprecatedAttribute" custom attribute to the "MyMethod" method in the "MyClass" class, indicating that the method is deprecated. Finally, we called the method and retrieved the custom attribute using reflection to obtain the deprecation message.

Use

Custom attributes are useful for providing metadata about types, methods, properties, and other elements in your code. You can use custom attributes to provide additional information about your code that can be used by tools and libraries to generate documentation, validate code, or configure your application.

Important Points

  • Custom attributes can be attached to types, methods, properties, and other elements in your code.
  • Custom attributes can have properties, fields, and methods to provide additional metadata.
  • You can retrieve custom attributes using reflection.

Summary

In this tutorial, we discussed how to create and use custom attributes in C#. We covered the syntax, example, output, explanation, use, and important points of custom attributes in C#. With this knowledge, you can now use custom attributes in your C# code to provide metadata that can be used by tools and libraries to generate documentation, validate code, or configure your application.

Published on: