Why is Attributes.IsDefined() missing overloads?

C#.NetReflection

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHans PassantView Question on Stackoverflow
Solution 1 - C#John LeidegrenView Answer on Stackoverflow