1

Why does the following work to find classes that are using my custom attribute but,

    Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    foreach(Assembly a in assemblies)
    {
        foreach(Type t in a.GetTypes())
        {
            object [] attributes = t.GetCustomAttributes(typeof(TestingAttribute), false);
            foreach(object o in attributes)
            {
                Debug.Log(o is TestingAttribute);
            }
        }
    }

this method and similar ones I've found on StackOverflow do not. This code is executed in Unity3d. The result is empty.

    Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    foreach(Assembly a in assemblies)
    {
        var attributes = a
                   .GetCustomAttributes(typeof(TestingAttribute), false)
                   .Cast<TestingAttribute>();
        foreach(TestingAttribute t in attributes)
        {
            Debug.Log(t);
        }
    }
fordeka
  • 979
  • 2
  • 7
  • 22
  • 3
    Surely the 2nd example is retrieving the attributes of the assembly itself, whereas the first enumerates the types in the assembly and gets those *types* that have that attribute...? – Liam Nov 28 '12 at 15:51
  • In that case is there any way to say "get all the custom attributes used in an assembly" or do you have to pull the custom attributes of every individual class? – fordeka Nov 29 '12 at 15:39
  • 1
    I don't think so. There is a neat solution using Linq [here](http://stackoverflow.com/questions/4852879/get-all-types-in-assembly-with-custom-attribute) though: – Liam Nov 29 '12 at 16:31

0 Answers0