C# Custom Attributes Example


The following example uses Linq to find all types in an assembly adorned with a particular custom attribute type. It should build and run on .NET Framework 3.5 and beyond. Note the use of AllowMultiple AttributeUsage.

using System;
using System.Linq;
using System.Reflection;

enum Version {
    Version1,
    Version2
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct,
   AllowMultiple = true)]
class VersionAttribute : Attribute
{
    public Version version;
}

[VersionAttribute(version = Version.Version1)]
[VersionAttribute(version = Version.Version2)]
class SomeClass1
{
}

[VersionAttribute(version = Version.Version1)]
class SomeClass2
{
}

class Program
{
    static void Main(string[] args)
    {
        // Linq query to retrieve all classes that are adorned with VersionAttribute
        Assembly assembly = Assembly.GetExecutingAssembly();
        var types = from type in assembly.GetTypes()
                    where Attribute.IsDefined(type, typeof(VersionAttribute))
                    select type;

        // print out information
        foreach (Type type in types)
        {
            Console.WriteLine("{0}", type.Name); // print class name
            var attrs = type.GetCustomAttributes(typeof(VersionAttribute), false);
            foreach (VersionAttribute version in attrs)
            {
                Console.WriteLine("\t{0}", version.version); // print version
            }
        }

        Console.ReadLine();
    }
}

Here’s the output of the program:

SomeClass1
        Version1
        Version2
SomeClass2
        Version1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s