如何获取 .NET 中的类列表

发布于 2024-11-04 04:33:27 字数 277 浏览 0 评论 0原文

我尝试了 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 技术交流群。

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

发布评论

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

评论(3

遗失的美好 2024-11-11 04:33:27

加载程序集,然后获取所有类型的列表:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom("System.Core.dll");
Type[] types = assembly.GetTypes();

不幸的是,如果无法加载任何公开的类型,这将引发异常,有时这种加载失败是无法避免的。然而,在这种情况下,抛出的异常包含已成功加载的所有类型的列表,因此我们可以这样做:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom("System.Core.dll");

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

这将为您提供所有类型的列表,包括接口、结构、枚举等...(如果您只想类,然后您可以过滤该列表)。

To load an assembly and then get a list of all types:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom("System.Core.dll");
Type[] types = assembly.GetTypes();

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:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom("System.Core.dll");

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

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).

以往的大感动 2024-11-11 04:33:27

如果可以引用 System.Core,则

    List<string> namespaces = new List<string>();

    var refs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

    foreach (var rf in refs) {
        if (rf.Name == "System.Core")
        {
            var ass = Assembly.Load(rf);
            foreach (var tp in ass.GetTypes())
            {
                if (!namespaces.Contains(tp.Namespace))
                {
                    namespaces.Add(tp.Namespace);
                    Console.WriteLine(tp.Namespace);
                }
            }
        }
    }

如果不能,则需要附加到 CurrentDomain 的 AssemblyResolve 事件,并加载 System.Core.dll 在加载 dll 时使用的所有类型的程序集。

If you can reference System.Core then

    List<string> namespaces = new List<string>();

    var refs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

    foreach (var rf in refs) {
        if (rf.Name == "System.Core")
        {
            var ass = Assembly.Load(rf);
            foreach (var tp in ass.GetTypes())
            {
                if (!namespaces.Contains(tp.Namespace))
                {
                    namespaces.Add(tp.Namespace);
                    Console.WriteLine(tp.Namespace);
                }
            }
        }
    }

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.

赴月观长安 2024-11-11 04:33:27

这是您问题的答案
我不需要复制&为您将其粘贴到此处,与从其他线程复制代码相比,节省空间可能会更环保。 :-)

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. :-)

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