为什么Attributes.IsDefined()缺少重载? [英] Why is Attributes.IsDefined() missing overloads?

查看:52
本文介绍了为什么Attributes.IsDefined()缺少重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到一个SO问题的启发.对于 IsDefined,属性类具有多个重载.()方法.涵盖了应用于Assembly,Module,MemberInfo,ParameterInfo的属性.MemberInfo重载涵盖PropertyInfo,FieldInfo,EventInfo,MethodInfo,ConstructorInfo.

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.

这可以处理大多数AttributeTargets.除了一个缺点:Attribute.IsDefined(Type,Type)没有重载,因此您可以检查是否在类上定义了属性.或针对此问题的结构,委托或枚举.

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.

这不是一个真正的问题,Type.GetCustomAttributes()可以解决此问题.但是所有的BlahInfo类型也都具有此功能.我不知道缺乏对称性.我不能指责为什么这会是Type的问题.猜测继承问题并不能向我解释.混合使用ValueType可能会领先,但这仍然没有道理.我不买他们忘了",他们从来不买.

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?

推荐答案

有一个 System.Attribute.IsDefined(MemberInfo元素,类型attributeType,布尔继承) System.Type 源自 System.Reflection.MemberInfo .

程序集,它是任何.NET程序集的顶级容器,具有一个或多个模块.然后,每个模块都包含类型,并且类型可以具有诸如属性,方法甚至其他类型(嵌套类型)的成员.这就是 System.Type 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.

这篇关于为什么Attributes.IsDefined()缺少重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆