如果我没记错的话,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/