我如何获得 .NET 中的类列表 [英] How do i get a list of classes in .NET

查看:35
本文介绍了我如何获得 .NET 中的类列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了 Assembly.ReflectionOnlyLoadFrom(@"path\System.Core.dll") 和 ReflectionOnlyLoad 但我遇到了异常和错误.如何正确获取程序集中的所有命名空间/类?

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.

推荐答案

如果可以参考 System.Core 则

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);
                }
            }
        }
    }

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

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.

这篇关于我如何获得 .NET 中的类列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆