How to get namespace from object name (as string)  
Author Message
jdang





PostPosted: .NET Base Class Library, How to get namespace from object name (as string) Top

Hi everyone,

I try to write a function to return a namespace based on the name of the input object ( as string).

For the most cases I can use Type.GetType(objName) to get the namespace back.

However, I have seen some special case like

"System.Diagnostics.Debug"

"System.Collections.Generic.List<string>" .... and Type.GetType(objName) just return null.

Are there any way to do this in C#

// Need help

private string GetNameSpace(string objName)

{

Type type = Type.GetType(objName);

if (type != null)

return type.Namespace;

else

{

//Example: input string

// "System.Diagnostics.Debug"

// "System.Collections.Generic.List<Car>"

// Assume the object name is valid and program can compliled

// Need a way to get the namespace for special cases

return "nameSpace";

}

}



.NET Development9  
 
 
Peter Huang - MSFT





PostPosted: .NET Base Class Library, How to get namespace from object name (as string) Top

Hi,

If we call the method as below,
str = "System.Diagnostics.Debug"
t = Type.GetType(str, true, true);

We will get error as below, because the Type.GetType method will only search the current assembly which called the method.
System.TypeLoadException: Could not load type 'System.Diagnostics.Debug' from assembly 'TestTraceDe****, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.RuntimeTypeHandle._GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, Boolean loadTypeFromPartialName)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.RuntimeType.PrivateGetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)

So we need to use Assembly.GetType approach to do that.
Because we did not know which assembly the type may locate, we can enumerate all the assembly in current appdomain.

static void Main(string[] args)
{
string str ="System.Diagnostics.Debug";
Type t=null;
Debug.WriteLine("Test"); //This line is necessary, or the System.dll will not loaded, so the "System.Diagnostics.Debug" will not be found.
try
{
Assembly[] myAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly myAssembly in myAssemblies)
{
Console.WriteLine(myAssembly.CodeBase.ToString());
t = myAssembly.GetType(str);
if (null != t)
break;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

if (null != t)
Console.WriteLine(t.Namespace.ToString());
}


If you still have any concern, please feel free to post here.

Best regards,
Peter Huang



 
 
Peter Huang - MSFT





PostPosted: .NET Base Class Library, How to get namespace from object name (as string) Top

Hi,

In addition, if you already know which assemly was the type located.
i.e. its AssemblyQualifiedName
You may also try to the code below.

Type t = Type.GetType("System.Diagnostics.Debug, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

http://msdn2.microsoft.com/en-us/system.type.assemblyqualifiedname.aspx

If you still have any concern, please feel free to post here.

Best regards,
Peter Huang



 
 
jdang





PostPosted: .NET Base Class Library, How to get namespace from object name (as string) Top

Hi Peter,

Thanks for you suggestion. It works for "System.Diagnostics.Debug".

However, type still equals null when I tried generic type like:

"System.Collections.Generic.List<T>",

"System.Collections.Generic.List<string>" or

"System.Collections.Generic.List<anyType>"

Regards,

JDang


 
 
Peter Huang - MSFT





PostPosted: .NET Base Class Library, How to get namespace from object name (as string) Top

Hi John,

For generic class, we have another string because it included the type we will include in the generic.

You may try to the method below.
Type classType = Type.GetType("System.Collections.Generic.List`1[System.String]",true);

Here is a link for your reference.
http://weblogs.asp.net/lancea/archive/2004/06/01/146202.aspx

Also to retrieve the full qualified name about the Type, we can run the code below.
System.Collections.Generic.List<string> sList = new List<string>();
Console.WriteLine(sList.GetType().AssemblyQualifiedName );

If you still have any concern, please feel free to post here.

Best regards,
Peter Huang