How do you get the root namespace of an assembly?

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

Given an instance of System.Reflection.Assembly.

13条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 12:25

    Adding to all the other answers here, hopefully without repeating information, here is how I solved this using Linq. My situation is similar to Lisa's answer.

    My solution comes with the following caveats:

    • You're using Visual Studio and have a Root Namespace defined for your project, which I assume is what you're asking for since you use the term "root namespace"
    • You're not embedding interop types from referenced assemblies
    Dim baseNamespace = String.Join("."c,
        Me.GetType().Assembly.ManifestModule.GetTypes().
            Select(Function(type As Type)
                        Return type.Namespace.Split("."c)
                    End Function
            ).
            Aggregate(Function(seed As String(), splitNamespace As String())
                            Return seed.Intersect(splitNamespace).ToArray()
                        End Function
            )
    )
    

提交回复
热议问题