Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Attributes.IsDefined() missing overloads?

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?

like image 652
Hans Passant Avatar asked Sep 27 '22 21:09

Hans Passant


1 Answers

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.

like image 171
John Leidegren Avatar answered Oct 24 '22 11:10

John Leidegren