views:

370

answers:

1

Inspired by an SO question. The Attribute class has several overloads for the IsDefined() method. Covered are attributes applied to Assembly, Module, MemberInfo, ParameterInfo. The MemberInfo overload covers PropertyInfo, FieldInfo, EventInfo, MethodInfo, ConstructorInfo.

That takes care of most of the AttributeTargets. Except for one biggy: there is no overload for Attribute.IsDefined(Type, Type) so that you could check if an attribute is defined on a class. Or a struct, delegate or enum for that matter.

Not that this is a real problem, Type.GetCustomAttributes() can fix that. But all of the BlahInfo types have this too. I wonder at the lack of symmetry. I can't put a finger on why this would be problem for Type. Guessing at an inheritance problem doesn't explain it to me. Having ValueType in the mix might be a lead, still doesn't make sense. I don't buy "they forgot", they never do.

Why is this overload missing?

+17  A: 

There's a System.Attribute.IsDefined(MemberInfo element, Type attributeType, bool inherit) and System.Type derives from System.Reflection.MemberInfo.

An assembly, which is the top-level container of any .NET assembly has one or more modules. Each module then contains types and types can have members such as properties, methods or even other types (nested types). That's why System.Type derives from MemberInfo so that the object model allows for types to work as containers all things members, including other types.

John Leidegren
Rats, Type derives from MemberInfo. Didn't see that coming, your answer is dead-on. Thanks :)
Hans Passant