所有内置的.Net属性

arc*_*rco 5 c# linq

我曾经用过AppDomain.CurrentDomain.GetAssemblies()列出所有程序集,但是如何使用C#列出.NET 2.0中的所有内置属性?

Jon*_*eet 15

请注意,这AppDomain.GetAssemblies()将只列出已加载的程序集......但是这很容易:

var attributes = from assembly in assemblies
                 from type in assembly.GetTypes()
                 where typeof(Attribute).IsAssignableFrom(type)
                 select type;
Run Code Online (Sandbox Code Playgroud)

.NET 2.0(非LINQ)版本:

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);
        }
    }                   
}
Run Code Online (Sandbox Code Playgroud)