By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,652 Members | 1,756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,652 IT Pros & Developers. It's quick & easy.

how do you find the root namespace at runtime?

P: n/a
Bob


Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Bob,
how do you find the root namespace at runtime? Root namespace of what?

You can get the namespace of a type, by looking at the methods & properties
of System.Type, specifically the Namespace property.

You can use Object.GetType to get the type of any object.

For example to get the namespace of a String variable, you can use:

Dim s As String = "Hello World"
Dim [namespace] As String = s.GetType().Namespace

Or the GetType keyword to get a System.Type from a type identifier.

For example to get the namespace of the main form class, you can use:

Dim [namespace] As String = GetType(MainForm).Namespace

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl... how do you find the root namespace at runtime?

Nov 21 '05 #2

P: n/a
Bob
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Bob,
how do you find the root namespace at runtime?

Root namespace of what?


The project that created the currently executing assembly.

In the property pages of a project, available by right-clicking one one and
selecting 'properties', under 'common properties'/'general' there is a place
you can enter a 'Root Namespace'. I would like access to this at runtime.

Bob
Nov 21 '05 #3

P: n/a
Bob,
Have you tried using my second example?

For example to get the namespace of the main form class, you can use:

Dim [namespace] As String = GetType(MainForm).Namespace

MainForm is the name of your startup object (as set in Project Properties).

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Bob,
> how do you find the root namespace at runtime?

Root namespace of what?


The project that created the currently executing assembly.

In the property pages of a project, available by right-clicking one one
and
selecting 'properties', under 'common properties'/'general' there is a
place
you can enter a 'Root Namespace'. I would like access to this at runtime.

Bob

Nov 21 '05 #4

P: n/a
Bob
You mean something like this -

Dim asm As [Assembly] = System.Reflection.Assembly.GetEntryAssembly
MsgBox(asm.EntryPoint.DeclaringType.Namespace)

But there is no guarantee that the assembly's startup object is not
contained within a deeper namespace than the root.

Um... hmmm. I guess this will work if I make sure to exclude any types from
referenced assemblies (not shown). The shortest Namespace will probably be
the root. Ugh, but again no guarantee...

Dim asm As [Assembly] = '<some assembly>
Dim Root As String
For Each t As Type In asm.GetTypes
If Root Is Nothing Then
Root = t.GetType.Namespace
Else
If Root.Length > t.FullName.Length Then
Root = t.FullName
End If
End If
Next
MsgBox(Root)

I guess I'll have to call this good enough.

Thanks,
Bob

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u9**************@TK2MSFTNGP15.phx.gbl...
Bob,
Have you tried using my second example?

For example to get the namespace of the main form class, you can use:

Dim [namespace] As String = GetType(MainForm).Namespace

MainForm is the name of your startup object (as set in Project Properties).
Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
Bob,
> how do you find the root namespace at runtime?
Root namespace of what?


The project that created the currently executing assembly.

In the property pages of a project, available by right-clicking one one
and
selecting 'properties', under 'common properties'/'general' there is a
place
you can enter a 'Root Namespace'. I would like access to this at runtime.
Bob


Nov 21 '05 #5

P: n/a
Bob,
You mean something like this -

Dim asm As [Assembly] = System.Reflection.Assembly.GetEntryAssembly
MsgBox(asm.EntryPoint.DeclaringType.Namespace) No I meant exactly what I showed! Of course the above doesn't require you to
know the startup class.
But there is no guarantee that the assembly's startup object is not
contained within a deeper namespace than the root. Then don't use the startup object, use a different class that is not
qualified with a namespace. I offered the startup object as its one known
object to exist in your project.
The shortest Namespace will probably be
the root. Ugh, but again no guarantee... Most of my projects have 2 or more namespaces for the root namespace, as the
root namespace tends to be: company.solution.project.
I guess I'll have to call this good enough. Bingo!

I have to ask: Does it really matter what the root namespace is? What do you
really need or want it for? (Do you really need it?)

As you found, there is no real guaranteed way of finding it. I offered the
GetType(SomeType).Namespace as it is "close enough" for most VB.NET
developers, of course it will fail in source files that include a Namespace
statement, the workaround of course is to put GetType(SomeType).Namespace in
SomeType, and do not explicitly put SomeType in a namespace.

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:eT**************@TK2MSFTNGP11.phx.gbl... You mean something like this -

Dim asm As [Assembly] = System.Reflection.Assembly.GetEntryAssembly
MsgBox(asm.EntryPoint.DeclaringType.Namespace)

But there is no guarantee that the assembly's startup object is not
contained within a deeper namespace than the root.

Um... hmmm. I guess this will work if I make sure to exclude any types
from
referenced assemblies (not shown). The shortest Namespace will probably be
the root. Ugh, but again no guarantee...

Dim asm As [Assembly] = '<some assembly>
Dim Root As String
For Each t As Type In asm.GetTypes
If Root Is Nothing Then
Root = t.GetType.Namespace
Else
If Root.Length > t.FullName.Length Then
Root = t.FullName
End If
End If
Next
MsgBox(Root)

I guess I'll have to call this good enough.

Thanks,
Bob

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u9**************@TK2MSFTNGP15.phx.gbl...
Bob,
Have you tried using my second example?

For example to get the namespace of the main form class, you can use:

Dim [namespace] As String = GetType(MainForm).Namespace

MainForm is the name of your startup object (as set in Project

Properties).

Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
> "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message > news:%2****************@TK2MSFTNGP11.phx.gbl...
>> Bob,
>> > how do you find the root namespace at runtime?
>> Root namespace of what?
>
> The project that created the currently executing assembly.
>
> In the property pages of a project, available by right-clicking one one
> and
> selecting 'properties', under 'common properties'/'general' there is a
> place
> you can enter a 'Root Namespace'. I would like access to this at runtime. >
> Bob
>
>



Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.