0

I'm create the class

public class MarkerAttribute : Attribute
{

}

and set this attribute in MyTestClass

[Marker]
public class MyTestsClass
{

}

How can I receive the names of all classes which have this attribute in my project?

Thank you.

  • You mean at runtime, by reflection, or do you mean searching in IDE tools etc.? – Rup Apr 07 '15 at 12:21

1 Answers1

0

You can get the types that have the attribute using reflection like this:

using System;
using System.Reflection;

public class MarkerAttribute : Attribute {
}

[Marker]
class Program {
    static void Main(string[] args) {
        foreach (Type type in Assembly.GetExecutingAssembly().GetTypes()) {
            foreach (Attribute attribute in type.GetCustomAttributes()) {
                MarkerAttribute markerAttribute = attribute as MarkerAttribute;
                if (markerAttribute != null) {
                    Console.WriteLine(type.FullName);
                }
            }
        }
    }
}
Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193