ReflectionUtil.isNameSpace(string name) - how to implement

  • Thread starter Thread starter Thomas Sondergaard
  • Start date 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
 
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.
 
Back
Top