If I do this, I enumerate over all types in my program:
List<SerializableAttribute> attributes=new List<SerializableAttribute>() ;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in assembly.GetTypes())
{
attributes.AddRange(
type.GetCustomAttributes(false)
.OfType<SerializableAttribute>()
.ToList());
}
}
Is metadata that comes with a .NET dll indexed to allow me to do something like:
List<SerializableAttribute> attributes = typeof(SerializableAttribute)
.GetClassesIAmDefinedOn();
Is there another option that I’m not considering?
(SerializableAttribute is just an example)
The most efficient thing to use here generally is
Attribute.IsDefined(...), although in the specific case of[Serializable],type.IsSerializableis faster (it isn’t actually stored as an attribute in this one case – it has special handling in the compiler, mapping to a CLI flag).