c# - Attribute.GetCustomAttributes 和 MemberInfo.GetCustomAttributes 可以互换吗?

标签 c# .net reflection attributes

如果我没记错的话,Attribute.GetCustomAttributes最初引入是因为 MemberInfo.GetCustomAttributes 字段和事件存在错误。

现在我试图找到任何指向此问题的链接,以了解它们在现代 .NET Framework 中是否完全可互换(如果此错误已修复),但我不能。我可能完全错了,那么请解释一下正确的历史,以及为什么有两种方法可以做同样的事情?

最佳答案

我错过了 documentation 中的此备注部分,这解释了一切:

Remarks

This method ignores the inherit parameter for properties and events. To search the inheritance chain for attributes on properties and events, use the appropriate overloads of the Attribute.GetCustomAttributes method.

我对字段的看法是错误的。此问题仅适用于属性和事件

希望这个问题能够改善这个问题的谷歌搜索能力。

关于c# - Attribute.GetCustomAttributes 和 MemberInfo.GetCustomAttributes 可以互换吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58863025/

相关文章:

c# - 将自定义日期格式(字符串)转换为日期时间

c# - 防止样式继承

c# - C#/.NET 是否有内置的 "VersionNumber"类?

.net - .NET Framework 的较高版本是否与较低版本兼容?

c# - 如何动态确定类型是否是使用反射的接口(interface)?

c# - 使用反射获取dll中某个基类型的所有类

java - 在 JPA 实体上查找声明的 java.util.Map 字段的泛型类型时出现 ClassNotFoundException (GlassFish 3)

c# - 如何选择 Windows 窗体文本框中的所有文本?

c# - 获取字符串的前 250 个单词?

.net - 在 32 位操作系统上编译的 .NET 程序可以在单声道下的 64 位 Linux 系统上运行而没有任何问题吗?