如何通过汇编获取项目中的所有命名空间?

How to get all namespace in project by assembly?

我想通过组装方法获取主项目中的所有命名空间。(不是全部)

我阅读了下面的问题,但没有用。

Getting all types in a namespace via reflection

例如

class 装配:

namespace test
{
    public class Class1
    {
        public string[] AllNameSpace()
        {
            return Assembly.GetExecutingAssembly().GetTypes().Select(x => x.Namespace).ToArray();
        }
    }
}

主项目中的代码:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var util=new Class1();
            Console.WriteLine(string.Join(",",util.AllNameSpace()));//return test
            Console.ReadKey();
        }
    }
}

return "test" 但我想包含 ConsoleApplication1 命名空间。

使用 Assembly.GetCallingAssembly 而不是 Assembly.GetExecutingAssembly,你应该得到 ConsoleApplication1-Assembly。

您获得了该程序集的所有命名空间,但它肯定会包含 ConsoleApplication1,但不仅如此。