473,412 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

how do you find the root namespace at runtime?

Bob


Nov 21 '05 #1
5 12324
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randy | last post by:
Is there a way to retrieve the root namespace during runtime? I'm trying to generalize a function and System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream() method requires...
4
by: Matty | last post by:
I have successfully written a couple of Web services that work fine on my localhost so this is not a security issue. My problem only arises when I set the Root Namespace of a Web service to...
3
by: Lee Moody | last post by:
Does anyone know where I can access programmically the value stored in the "Root Namespace" parameter of the project's common properties? To find this within the IDE, select your project name...
2
by: Rafael Pivato | last post by:
Can I have two Class Libraries to share the same root namespace ? I want something like this: MySystem (root namespace) MyBO (class library A) MyDS (class library B) MyUS (class library C)
3
by: CCJohn | last post by:
Hi, I find that while I can use the "Namespace...End Namespace" inside my code, there is also a "Root namespace" field in the project properties dialog box under General. So where should I put...
2
by: Jeff Brown | last post by:
Hi, I suspect that this isn't possible, but I figured I'd ask. My project has a root namespace, let's say it's "Root", that applies to almost every source file (which is why I've set it as the...
6
by: Viet | last post by:
Is it possible to instantiate an object from the Root namespace? I have a MS Visual Studio 2002 vb.net project consisting of a dozen individual project files and I would like to know how to...
4
by: Brian Henry | last post by:
Is there anyway to get the name of the rootname space of a loaded assembly back as a string? thanks
2
by: gabe | last post by:
Hi, We have a number of projects that will use the default 'Root Namespace' option in the project properties box. There will be a common Master Page that will be used among the projects. I'd...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.