Assembly.GetTypes - 如果 GetExportedTypes 可用,为什么要使用它?
我对您会使用其中一种或另一种的场景感到困惑。
如果您有一个包含一些公共和私有(或内部)类型的程序集,那么只有公共类型应该从外部可用。任何内部类型或私有类型都不应该可用,事实上,它们的存在不应该被发现。
因此,在我看来,GetTypes
和 GetExportedTypes
应该返回相同的内容。
显然我的想法是错误的——每一个的用途是什么?
谢谢!
I'm confused about what scenarios you would use one or the other.
If you have an assembly with a some public and private (or internal) types in it, then only the public types should be available from outside. Any types that are internal, or private - should not be available, in fact, their existence should not be discoverable.
Therefore, GetTypes
and GetExportedTypes
- in my mind, should return the same thing.
Clearly I'm thinking about this wrong - what is each one for?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自 MSDN 文档:
来自 MSDN 文档:
因此,
GetTypes()
调用确实会为您提供程序集中定义的所有类型 - 无论它们是否对您“可见”且可实例化。可能看起来很奇怪 - 但如果您想检查自己、您自己的程序集(或与您的代码位于同一命名空间中的程序集)怎么办?如果需要的话,您需要能够看到一切。From the MSDN docs:
From the MSDN docs:
So the
GetTypes()
call will indeed give you all types defined in an assembly - whether they're "visible" and instantiable to you or not. Might seem odd - but what if you want to inspect yourself, your own assembly (or an assembly in the same namespace as your code)? You need to be able to see everything - if needed.语言级别的可见性与反射级别的类型可见性无关。
反射的整个想法是你可以看到所有类型、成员等并检查它们;比如说用于代码生成目的或其他目的。同样,您也可能遇到使用
InternalsVisibleToAttribute
的情况,以及正如其他人所说,当您需要反映自己的程序集时。这些都是完全合法的,如果不可用的话,这些都是不可能的(从而严重限制 .Net 框架)。因此,默认应该是返回所有类型 - 仅当在运行时尝试使用某个类型时,可见性才会出现。也可以侧步; .Net框架本身依赖于一些能够实例化其他程序集自己的私有类型的场景;您也可以跳过对自己动态构建的程序集的可见性检查。我在为我们的内部应用程序编写的自定义 IOC 和 DI 框架上使用此功能,以允许我们的开发人员使类型完全对外部代码隐藏,但仍可在其应用程序中使用。
Visibility at the language level has nothing to do with type visibility at the reflection level.
The whole idea of reflection is that you can see all types, members etc and inspect them; say for code generation purposes or whatever. Equally you have scenarios such as where the
InternalsVisibleToAttribute
is used and, as others have said, when you need to reflect your own assembly. These are all perfectly legitimate and would be made impossible (thereby heavily restricting the .Net framework) if not available.Therefore the default should be to return all types - only when an attempt is made to use a type at runtime does visibility come into it. It can also be side-stepped; the .Net framework itself relies on some scenarios where being able to instantiate other assemblys' own private types; and you can skip visibility checks on your own dynamically built assemblies too. I use this feature on my own custom-rolled IOC and DI framework written for our in-house applications to allow our devs to make types completely hidden from external code, but still useable within their applications.
GetExportedTypes()
不包括受保护/私有类型。GetTypes()
包括所有类型。关于
internal
类型GetExportedTypes()
不清楚。GetExportedTypes()
does not include protected/private types.GetTypes()
includes all Types.Regarding
internal
types the MSDN documentation ofGetExportedTypes()
is unclear.我正在调查一个遇到此问题的 Web API bug,并发现
Assembly.GetExportedTypes
和“Assembly.GetTypes”。它在文档中,但不是很清楚。如果无法加载任何依赖程序集,
Assembly.GetExportedTypes
将引发“FileNotFoundException”。Assembly.GetTypes
抛出ReflectionTypeLoadException
,其中包含成功加载的类型。因此,如果您希望在程序集中的某些类型无法加载的情况下也能成功,则应该使用GetTypes
而不是GetExportedTypes
。这段代码不起作用,因为
Assembly.GetExportedTypes
不会抛出ReflectionTypeLoadException
。I was investigating a web API bug that ran into this and found one very important difference between
Assembly.GetExportedTypes
and 'Assembly.GetTypes`. It is in the documentation but is not very clear.Assembly.GetExportedTypes
throws a 'FileNotFoundException' if any of the dependent assemblies cannot be loaded.Assembly.GetTypes
throwsReflectionTypeLoadException
which contains types that are loaded successfully. So, if you want to succeed even if some of the types in the assembly cannot be loaded, you should useGetTypes
and notGetExportedTypes
.This piece of code would not work as
Assembly.GetExportedTypes
does not throwReflectionTypeLoadException
.对于外部程序集确实是这种情况,但是如果您在自己的程序集中调用 GetTypes 会怎么样?
然后你也会看到私有的和内部的,这是合乎逻辑的。
For external assemblies that would indeed be the case, but what if you call GetTypes on your own assembly?
Then you'd see the private and internal ones too, which is logical.
如果您需要所有公共类型,则可以使用 GetExportedTypes,但如果您还需要所有其他类型,则可以使用 GetTypes。即使类型是私有的或内部的,您也可以创建新实例并通过反射和动态使用它,所以我不认为这两者是多余的。
If you want all the public types, you can use GetExportedTypes, but if you want all the other types as well, you can use GetTypes. Even if a type is private or internal you can create new instances and use it via Reflection and Dynamic, so I don't see any of these two being redundant.