0
0
C Sharp (C#)programming~5 mins

Reading attributes with reflection in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Sometimes, you want to get extra information about your code while it runs. Reading attributes with reflection lets you do that by checking special notes (attributes) on your code parts.

You want to check if a class or method has a special tag before running some code.
You need to get settings or metadata added as attributes to customize behavior.
You want to build tools that work with code details, like serializers or validators.
Syntax
C Sharp (C#)
var type = typeof(YourClass);
var attributes = type.GetCustomAttributes(typeof(YourAttribute), inherit: true);
foreach (var attr in attributes)
{
    var yourAttr = (YourAttribute)attr;
    // Use yourAttr properties here
}

typeof(YourClass) gets the type information of the class.

GetCustomAttributes returns all attributes of the specified type.

Examples
This example reads the Obsolete attribute message from a class.
C Sharp (C#)
[Obsolete("Use NewMethod instead")]
class OldClass {}

var attrs = typeof(OldClass).GetCustomAttributes(typeof(ObsoleteAttribute), true);
foreach(var attr in attrs)
{
    var obsolete = (ObsoleteAttribute)attr;
    Console.WriteLine(obsolete.Message);
}
This checks if the Serializable attribute is present on the class.
C Sharp (C#)
[Serializable]
class Data {}

bool isSerializable = typeof(Data).IsDefined(typeof(SerializableAttribute), true);
Console.WriteLine(isSerializable);
Sample Program

This program defines a custom attribute InfoAttribute with a description. It adds this attribute to a class and a method. Then it uses reflection to read and print the descriptions.

C Sharp (C#)
using System;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
class InfoAttribute : Attribute
{
    public string Description { get; }
    public InfoAttribute(string description) => Description = description;
}

[Info("This is a sample class")]
class SampleClass
{
    [Info("This is a sample method")]
    public void SampleMethod() {}
}

class Program
{
    static void Main()
    {
        var type = typeof(SampleClass);
        var classAttrs = type.GetCustomAttributes(typeof(InfoAttribute), true);
        foreach (InfoAttribute attr in classAttrs)
        {
            Console.WriteLine($"Class attribute description: {attr.Description}");
        }

        var method = type.GetMethod("SampleMethod");
        var methodAttrs = method.GetCustomAttributes(typeof(InfoAttribute), true);
        foreach (InfoAttribute attr in methodAttrs)
        {
            Console.WriteLine($"Method attribute description: {attr.Description}");
        }
    }
}
OutputSuccess
Important Notes

Reflection can slow down your program if used too much, so use it only when needed.

Attributes can be applied to many code parts like classes, methods, properties, and more.

Summary

Reflection lets you read extra notes (attributes) added to your code.

You can check attributes on classes, methods, and other parts at runtime.

This helps customize behavior or build flexible tools.