tags:

views:

110

answers:

1

I have used AppDomain.CurrentDomain.GetAssemblies() to list all assemblies, but how do I list all built-in attributes in .NET 2.0 using C#?

+10  A: 

Note that AppDomain.GetAssemblies() will only list the loaded assemblies... but then it's easy:

var attributes = from assembly in assemblies
                 from type in assembly.GetTypes()
                 where typeof(Attribute).IsAssignableFrom(type)
                 select type;

.NET 2.0 (non-LINQ) version:

List<Type> attributes = new List<Type>();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    foreach (Type type in assembly.GetTypes())
    {
        if (typeof(Attribute).IsAssignableFrom(type))
        {
            attributes.Add(type);
        }
    }                   
}
Jon Skeet
I'd give +2 for mentioning that it only lists loaded assemblies if I could!
Will
@Jon: added 2.0 version. @arco: you really start to appreciate linq when you have to go back to the old way.
Michael Petrotta
@Michael: Thanks, spot on.
Jon Skeet