ReflectionUtil.isNameSpace(string name) - how to implement

  • Thread starter Thomas Sondergaard
  • Start date
T

Thomas Sondergaard

I'd like to hear sugestions for how you would go about implementing this
function


public class ReflectionUtil {

public static bool isNameSpace(string name) {
// return true if 'name' is a namespace in any of
// the loaded assemblies
}

}

I understand that namespaces are not something .net understands, it is
simply something C# (and many other .net languages) defines and as far as
the .net runtime is concerned they just end up being part of the fully
qualified names of the types that are defined in assemblies. Well, all the
same there must be a way, possibly computationally expensive, to check
whether a given string 'name' indicates a namespace.

Test cases (may not compile)


public void testIsNameSpace() {
// Assuming at least mscorlib.dll is loaded so the System.* stuff is
available
Assert(ReflectionUtil.isNameSpace("System"));
Assert(ReflectionUtil.isNameSpace("System.Collections"));
Assert(ReflectionUtil.isNameSpace("System.Runtime.InteropServices"));
Assert(!ReflectionUtil.isNameSpace("NoSuchNameSpace"));
Assert(!ReflectionUtil.isNameSpace("System.NotThereEither"));
}

Any ideas. Code is welcome :)

Cheers,

Thomas
 
J

Jon Skeet

Thomas Sondergaard said:
I'd like to hear sugestions for how you would go about implementing this
function

public class ReflectionUtil {

public static bool isNameSpace(string name) {
// return true if 'name' is a namespace in any of
// the loaded assemblies
}

}

Well, you can use AppDomain.GetAssemblies to find the loaded
assemblies, Assembly.GetTypes to get the types in each assembly, and
Type.Namespace to get the namespace for a type.

Just go through all of the types in all of the assemblies in the
AppDomain and see whether any match the namespace requested.
 

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