Assembly.GetTypes - 如果 GetExportedTypes 可用,为什么要使用它?

发布于 2024-12-09 04:18:07 字数 239 浏览 0 评论 0原文

我对您会使用其中一种或另一种的场景感到困惑。

如果您有一个包含一些公共和私有(或内部)类型的程序集,那么只有公共类型应该从外部可用。任何内部类型或私有类型都不应该可用,事实上,它们的存在不应该被发现。

因此,在我看来,GetTypesGetExportedTypes 应该返回相同的内容。

显然我的想法是错误的——每一个的用途是什么?

谢谢!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

月亮邮递员 2024-12-16 04:18:07

来自 MSDN 文档

Assembly.GetTypes 方法
返回值类型:System.Type[]
包含此程序集中定义的所有类型的数组。

来自 MSDN 文档

Assembly.GetExportedTypes 方法
返回值
类型:System.Type[]
一个数组,表示此程序集中定义的类型,这些类型在程序集外部可见。

因此,GetTypes() 调用确实会为您提供程序集中定义的所有类型 - 无论它们是否对您“可见”且可实例化。可能看起来很奇怪 - 但如果您想检查自己、您自己的程序集(或与您的代码位于同一命名空间中的程序集)怎么办?如果需要的话,您需要能够看到一切。

From the MSDN docs:

Assembly.GetTypes Method
Return Value Type: System.Type[]
An array that contains all the types that are defined in this assembly.

From the MSDN docs:

Assembly.GetExportedTypes Method
Return Value
Type: System.Type[]
An array that represents the types defined in this assembly that are visible outside the assembly.

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.

潦草背影 2024-12-16 04:18:07

语言级别的可见性与反射级别的类型可见性无关。

反射的整个想法是你可以看到所有类型、成员等并检查它们;比如说用于代码生成目的或其他目的。同样,您也可能遇到使用 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.

等待圉鍢 2024-12-16 04:18:07

GetExportedTypes() 不包括受保护/私有类型。 GetTypes() 包括所有类型。

关于 internal 类型 GetExportedTypes() 不清楚。

GetExportedTypes() does not include protected/private types. GetTypes() includes all Types.

Regarding internal types the MSDN documentation of GetExportedTypes() is unclear.

花桑 2024-12-16 04:18:07

我正在调查一个遇到此问题的 Web API bug,并发现 Assembly.GetExportedTypes 和“Assembly.GetTypes”。它在文档中,但不是很清楚。

如果无法加载任何依赖程序集,Assembly.GetExportedTypes 将引发“FileNotFoundException”。 Assembly.GetTypes 抛出 ReflectionTypeLoadException,其中包含成功加载的类型。因此,如果您希望在程序集中的某些类型无法加载的情况下也能成功,则应该使用 GetTypes 而不是 GetExportedTypes

这段代码不起作用,因为 Assembly.GetExportedTypes 不会抛出 ReflectionTypeLoadException

        Type[] types;
        try
        {
            types = assembly.GetExportedTypes();
        }
        catch (ReflectionTypeLoadException e)
        {
            types = e.Types;
        }

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 throws ReflectionTypeLoadException 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 use GetTypes and not GetExportedTypes.

This piece of code would not work as Assembly.GetExportedTypes does not throw ReflectionTypeLoadException.

        Type[] types;
        try
        {
            types = assembly.GetExportedTypes();
        }
        catch (ReflectionTypeLoadException e)
        {
            types = e.Types;
        }
箜明 2024-12-16 04:18:07

对于外部程序集确实是这种情况,但是如果您在自己的程序集中调用 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.

带刺的爱情 2024-12-16 04:18:07

如果您需要所有公共类型,则可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文