Retrieving root namespace from within a module

O

Oenone

I am trying to write a generic piece of code that I can copy between
projects that uses the current project's root namespace as part of its
function.

I can easily retrieve this value when I am running in a class by doing the
following:

\\\
Dim namespace As String = Me.GetType.Namespace
///

This returns the value I am looking for. However, I want to be able to place
this code into a module. Me.GetType is not valid in this circumstance as
'Me' cannot be used within a module.

I can get this working by creating an instance of an arbitrary object within
my project and calling GetType on it, but as the classes differ from one
project to another I won't be able to copy-and-paste the code without having
to make changes to it.

Is there an easy way to do what I'm trying to do?

Thanks,
 
A

Armin Zingler

Oenone said:
I am trying to write a generic piece of code that I can copy between
projects that uses the current project's root namespace as part of its
function.

I can easily retrieve this value when I am running in a class by doing the
following:

\\\
Dim namespace As String = Me.GetType.Namespace
///

This returns the value I am looking for. However, I want to be able to place
this code into a module. Me.GetType is not valid in this circumstance as
'Me' cannot be used within a module.

I can get this working by creating an instance of an arbitrary object within
my project and calling GetType on it, but as the classes differ from one
project to another I won't be able to copy-and-paste the code without having
to make changes to it.

Is there an easy way to do what I'm trying to do?

From yesterday:

http://groups.google.com/[email protected]


In a shared method, you can use

System.Reflection.MethodBase.GetCurrentMethod.DeclaringType

to get the current type.

Armin
 
H

Herfried K. Wagner [MVP]

Oenone said:
I am trying to write a generic piece of code that I can copy between
projects that uses the current project's root namespace as part of its
function.

I can easily retrieve this value when I am running in a class by doing the
following:

\\\
Dim namespace As String = Me.GetType.Namespace
///

This returns the value I am looking for. However, I want to be able to
place this code into a module. Me.GetType is not valid in this
circumstance as 'Me' cannot be used within a module.

\\\
Dim s As String = GetType(Module1).Namespace
///
 
O

Oenone

Armin said:
In a shared method, you can use

System.Reflection.MethodBase.GetCurrentMethod.DeclaringType

to get the current type.

That's perfect, many thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top