Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All built-in .Net attributes

Tags:

c#

linq

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#?

like image 691
arco Avatar asked Oct 08 '10 06:10

arco


People also ask

What are assembly attributes?

Assembly attributes are values that provide information about an assembly. The attributes are divided into the following sets of information: Assembly identity attributes. Informational attributes. Assembly manifest attributes.

What are .NET attributes?

Attributes are used for adding metadata, such as compiler instruction and other information such as comments, description, methods and classes to a program. The . Net Framework provides two types of attributes: the pre-defined attributes and custom built attributes.

What are the attributes in C#?

In C#, attributes are classes that inherit from the Attribute base class. Any class that inherits from Attribute can be used as a sort of "tag" on other pieces of code. For instance, there is an attribute called ObsoleteAttribute . This is used to signal that code is obsolete and shouldn't be used anymore.


1 Answers

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);
        }
    }                   
}
like image 99
Jon Skeet Avatar answered Oct 02 '22 20:10

Jon Skeet