问题描述
.NET 的反射 API 使用数组而不是索引器来访问类型的成员/方法/属性/等有什么原因吗?我了解添加集合 类(例如 MemberInfoCollection
、MethodInfoCollection
等)的开销.但是,这些集合 类可以根据需要 实例化.设计原理是否不仅仅是劝阻程序员使用反射,除非他们真的需要它"?
Is there any reason why .NET's Reflection API uses arrays instead of indexers to access a type's members/methods/properties/etc.? I understand the overhead of adding collection classes such as MemberInfoCollection
, MethodInfoCollection
, etc. However, these collection classes could be instantiated on demand. Was the design rationale something other than just "dissuade programmers from using Reflection unless they really need it"?
推荐答案
这些 API 是在 .Net 1.0 中创建的,没有泛型.
These APIs were created in .Net 1.0, which didn't have generics.
他们不能只返回一个 ReadOnlyCollection
,而且他们懒得为他们需要返回的每种类型创建一个单独的类型化集合类.
They couldn't just return a ReadOnlyCollection<MemberInfo>
, and they were too lazy to create a separate typed collection class for each type they need to return.
(我没有这种信念的来源)
(I don't have a source for this belief)
这篇关于.NET 的反射 API 使用数组而不是索引器来检索类型元数据有什么原因吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!