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#?
views:
110answers:
1
+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
2010-10-08 06:46:04
I'd give +2 for mentioning that it only lists loaded assemblies if I could!
Will
2010-10-08 06:53:43
@Jon: added 2.0 version. @arco: you really start to appreciate linq when you have to go back to the old way.
Michael Petrotta
2010-10-08 07:38:33
@Michael: Thanks, spot on.
Jon Skeet
2010-10-08 08:07:25