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

例如我得到了这个例外。

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

最佳答案

如果可以引用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事件,并加载在加载dll时System.Core.dll使用的所有类型的程序集。

关于c# - 如何获取.NET中的类列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5827562/

10-12 17:34
查看更多