Consider the case when the assembly contains one or more types associated with the MyAttribute user attribute, and you need to get a list of these types. Is there any benefit to using IsDefined against GetCustomAttributes besides a more compact syntax? To show / hide what the other does not have? Is more effective than the other?
Here is a sample code demonstrating each use:
Assembly assembly = ... var typesWithMyAttributeFromIsDefined = from type in assembly.GetTypes() where type.IsDefined(typeof(MyAttribute), false) select type; var typesWithMyAttributeFromGetCustomAttributes = from type in assembly.GetTypes() let attributes = type.GetCustomAttributes(typeof(MyAttribute), false) where attributes != null && attributes.Length > 0 select type;
reflection c # attributes
Jay walker
source share