How do you get the root namespace of an assembly?

前端 未结 13 1961
后悔当初
后悔当初 2020-12-01 11:41

Given an instance of System.Reflection.Assembly.

相关标签:
13条回答
  • 2020-12-01 12:14

    There could be any number of namespaces in a given assembly, and nothing requires them to all start from a common root. The best you could do would be to reflect over all the types in an assembly and build up a list of unique namespaces contained therein.

    0 讨论(0)
  • 2020-12-01 12:15

    I just created an empty internal class called Root and put it in the project root (assuming this is your root namespace). Then I use this everywhere I need the root namespace:

    typeof(Root).Namespace;
    

    Sure I end up with an unused file, but it's clean.

    0 讨论(0)
  • 2020-12-01 12:15

    Assemblies don't necessarily have a root namespace. Namespaces and Assemblies are orthogonal.

    What you may be looking for instead, is to find a type within that Assembly, and then find out what its namespace is.

    You should be able to accomplish this by using the GetExportedTypes() member and then using the Namespace property from one of the returned Type handles.

    Again though, no guarantees all the types are in the same namespace (or even in the same namespace hierarchy).

    0 讨论(0)
  • 2020-12-01 12:19

    Get Types gives you a list of Type objects defined in the assembly. That object has a namespace property. Remember that an assembly can have multiple namespaces.

    0 讨论(0)
  • 2020-12-01 12:19
    GetType(frm).Namespace
    

    frm is the startup Form

    0 讨论(0)
  • 2020-12-01 12:19

    There actually is an indirect way to get it, by enumerating the names of the assembly's manifest resources. The name you want ends with the part of it that you know.

    Rather than repeat the code here, please see get Default namespace name for Assembly.GetManifestResourceStream() method

    0 讨论(0)
提交回复
热议问题