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#?
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.
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.
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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With