如何获取 .NET 中的类列表
我尝试了 Assembly.ReflectionOnlyLoadFrom(@"path\System.Core.dll") 和 ReflectionOnlyLoad 但出现异常和错误。如何正确获取程序集中的所有命名空间/类?
例如我得到了这个例外。
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
I tried Assembly.ReflectionOnlyLoadFrom(@"path\System.Core.dll")
and ReflectionOnlyLoad but i got exceptions and errors. How do i properly get all the namespaces/classes in an assembly?
For example i got this exception.
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
加载程序集,然后获取所有类型的列表:
不幸的是,如果无法加载任何公开的类型,这将引发异常,有时这种加载失败是无法避免的。然而,在这种情况下,抛出的异常包含已成功加载的所有类型的列表,因此我们可以这样做:
这将为您提供所有类型的列表,包括接口、结构、枚举等...(如果您只想类,然后您可以过滤该列表)。
To load an assembly and then get a list of all types:
Unfortunately this will throw an exception if any of the types exposed cannot be loaded, and sometimes this load failure cannot be avoided. In this case however the thrown exception contains a list of all types that were successfully loaded and so we can just do this:
This will give you a list of all types, including interfaces, structs, enums etc... (If you want just the classes then you can filter that list).
如果可以引用 System.Core,则
如果不能,则需要附加到 CurrentDomain 的 AssemblyResolve 事件,并加载 System.Core.dll 在加载 dll 时使用的所有类型的程序集。
If you can reference System.Core then
If you cannot, you will need to attach to the AssemblyResolve event of the CurrentDomain and load all assemblies of types that System.Core.dll uses when loading the dll.
这是您问题的答案。
我不需要复制&为您将其粘贴到此处,与从其他线程复制代码相比,节省空间可能会更环保。 :-)
Here is your answer to your question.
I do not need to copy & paste it here for you, it might be greener to save space rather that copying code from the other thread. :-)