Root namespace

  • Thread starter Thread starter Bob Altman
  • Start date Start date
B

Bob Altman

Hi all,

Is there a way to programmatically determine the root namespace for an
assembly?

TIA,

- Bob
 
Bob Altman said:
Is there a way to programmatically determine the root namespace for an
assembly?

There's no such thing, really - root namespace is a Visual Studio
concept, not a .NET concept.
 
I guess I should add that I'm trying to write a general-purpose routine that
fetches a string from a resource file named "strings.resx" in my caller's
assembly. Problem is, in order to get a ResourceManager, I need a resource
name of the form:

<root namespace>.strings.resx

I guess I could look at all of the resource names in the calling assembly
and use the first one that ends with ".strings.resx", but that's really
tacky...
 
There's no such thing, really - root namespace is a Visual Studio
concept, not a .NET concept.

I know. I'm fishing for some way to guess the root namespace. For example,
is there something that VB always puts into the assembly (like a type or an
attribute) from whose name I can derive the root namespace?

- Bob
 
Bob Altman said:
I know. I'm fishing for some way to guess the root namespace. For example,
is there something that VB always puts into the assembly (like a type or an
attribute) from whose name I can derive the root namespace?

Not that I know of, I'm afraid.
 
Hi Bob,

Can you post some code about what do you wants to do?
Are you using a ResXResourceReader to read the resx file directly
Or using ResourceManager to read resource from an assembly.

Also have you try to declare an instance in the loaded assembly and call
the gettype().tostring to return the full name, is that what you want?

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

I'm using ResourceManager.

By convention, my VB apps will all have a resource file that shows up in
Solution Explorer as "strings.resx". I want to write a generic GetString
routine that lives in a library DLL that knows how to find the "strings"
resource in its caller's assembly and, using ResrouceManager, fetch the
value of a specified resource string. My problem is, VB insists on
qualifying "strings.resource" with the root namespace.

In the best possible solution, the routine that calls GetString would not
need to specify the root namespace (either directly or indirectly).
GetString has no problem getting a reference to the caller's assembly. But
there may possibly be more than one resource in that assembly whose name
ends with ".strings.resource".

So... I'm fishing for something that I can look for in the caller's assembly
from whose name I can reliably deduce the root namespace, and thereby
reliably fabricate the full name for <RootNamespace>.strings.resource

- Bob
 
Bob said:
Hi Peter,

I'm using ResourceManager.

By convention, my VB apps will all have a resource file that shows up in
Solution Explorer as "strings.resx". I want to write a generic GetString
routine that lives in a library DLL that knows how to find the "strings"
resource in its caller's assembly and, using ResrouceManager, fetch the
value of a specified resource string. My problem is, VB insists on
qualifying "strings.resource" with the root namespace.

In the best possible solution, the routine that calls GetString would not
need to specify the root namespace (either directly or indirectly).
GetString has no problem getting a reference to the caller's assembly. But
there may possibly be more than one resource in that assembly whose name
ends with ".strings.resource".

So... I'm fishing for something that I can look for in the caller's assembly
from whose name I can reliably deduce the root namespace, and thereby
reliably fabricate the full name for <RootNamespace>.strings.resource

I don't think you can reliably deduce the root namespace, other than by
observing the fact that the assembly contains types that always start
with that particular name. This can be true of other assemblies as
well, though.

I'd suggest something along the lines of:

- if "strings.resource" exists, then load it and you're done;

- else, find all the names of the form "*.strings.resource", using
a regular expression, for example. If there's only one then load that
and you're done;

- else, throw an exception, or load the first one in the list, or
load one where the first part of the name matches the assembly name, or
.... What you decide to do in this case depends on a policy that your
generic GetString() method would document.

To handle cases where the default policy doesn't fit a particular user's
situation, overload GetString() to take a 'rootnamespace' parameter.
 
Thanks mikeb. See, this goes to show, you never know when you'll get a
suggestion that helps... It just never occurred to me to use the assembly
name itself to determine the root namespace. I'm happy to make a big, fat
assumption that the root namespace is either identical to the assembly name
(this is VB's default behavior), or that the root namespace ends with the
assembly name (assuming my caller has prefixed the root namespace with the
company and/or project names).

- Bob
 
Hi all,

Is there a way to programmatically determine the root namespace for an
assembly?

TIA,

- Bob
Would this work?

static string GetNamespace(Assembly asm)
{
Type[] t=asm.GetTypes();
if (t.Length>0)
{
string name=t[0].FullName;
int i=name.IndexOf('.');
if (i>-1)
return name.Substring(0,i);
}
return null;
}
Austin
 
Bob said:
Thanks mikeb. See, this goes to show, you never know when you'll get a
suggestion that helps... It just never occurred to me to use the assembly
name itself to determine the root namespace. I'm happy to make a big, fat
assumption that the root namespace is either identical to the assembly name
(this is VB's default behavior), or that the root namespace ends with the
assembly name (assuming my caller has prefixed the root namespace with the
company and/or project names).

Just be careful - a VB.NET project's root namespace defaults to the
project name, which is also the default for the assembly name. However,
each of those things can be changed.

So if you're going to make an assumption like that, be sure to document it.

I'd suggest using the assembly name as a hint only in the case where
there's more than one resource name that ends in ".strings.resource".
I'd suspect that that is a very rare situation.
 
I'd suggest using the assembly name as a hint only in the case where
there's more than one resource name that ends in ".strings.resource".
I'd suspect that that is a very rare situation.

I agree. Thanks for pointing me in the right direction!

- Bob
 
Hi Austin,

Thanks for the suggestion, but... That doesn't work, for a couple of
reasons. VB qualifies all type names with the root namespace, which might
contain more than one "." character. (Your code would have been more
correct, but still not completely correct, if you had used .LastIndexOf() to
find the last "." instead of the first one.)

The more basic problem is that nothing prevents a VB developer from further
qualifying a type name by explicitly tacking on extra namespace terms. For
example, suppose my root namespace is "MyCompany.MyApp", and I have the
following code:

Namespace Bob
Public Class SomeClass
End Class
End Namespace

The fully qualified name for this class is "MyCompany.MyApp.Bob.SomeClass".
Your routine has no way to determine what part of that name is the "root
namespace".

- Bob
 
Back
Top