System.GetType returns null

A

audipen

I have a problem with System.Type.GetType method.

If you try out the following code in C# console app ..

System.Type t = System.Type.GetType("System.DateTime");
System.Type t1 = System.Type.GetType("DateTime");

t is set to the appropriate Type object
but the second call returns null (when I dont specify the namespace
name)

The documentation mentions that the current assembly and Mscorlib.dll
is searched...but I dont think that 'Mscorlib.dll' is being searched.

Any help would be highly appreciated.

Thanks,
Ady
 
J

Jon Skeet [C# MVP]

audipen said:
I have a problem with System.Type.GetType method.

If you try out the following code in C# console app ..

System.Type t = System.Type.GetType("System.DateTime");
System.Type t1 = System.Type.GetType("DateTime");

t is set to the appropriate Type object
but the second call returns null (when I dont specify the namespace
name)

The documentation mentions that the current assembly and Mscorlib.dll
is searched...but I dont think that 'Mscorlib.dll' is being searched.

Yes it is - but as you haven't specified the full type name, it's not
finding it. You need to be very clear about the difference between
namespaces and assemblies. The comment in the docs states which
assemblies are searched when the assembly isn't specified.

The docs aren't very clear, but basically you *do* have to include the
namespace unless the type is in the "global" namespace.
 
K

Keith Patrick

This is one of those reasons I don't like overloaded methods in an API for
the purpose of default parameters. It would have been more obvious what was
going on if .GetType("DateTime", true) was the only way to call it, as the
former method call masks the reason why the type didn't exist, being that
null can mean many things on a search-type call.
 
M

MUS

Hello audipen !

You need to keep an eye on matter that "namespace" and "assembly" are
not related to each other in any way (It was used to be a case in Java
where package was strongly binded to the file structure but not here in
..Net).

Various types (classes) belonging to a particular namespace might be
implemented in multiple assemblies. For example
"System.Collections.ArrayList" type is in "mscorlib.dll" assembly and
the "System.Collections.StringCollection" type is in "system.dll"
assembly. So you see here "System.Collections" namespace is spaned
across two assemblies "mscorlib.dll" and "system.dll"

On the contrary, a single assembly can contain types belonging to
heterogeneous namespaces. For example "System.Int32" and
"System.Collections.ArrayList" types are both in "mscorlib.dll". Here
"mscorlib.dll" contains both the "System" namespace as well as the
"System.Collections" namespace.

If you go through MSDN you will notice that the documentation expressly
specify:
1) The namespace that the type belongs to i.e. "ArrayList" type belongs
to namespace "System.Collections", and
2) Which assembly contains the type i.e. "mscorlib.dll" contains
"System.Collections.ArrayList"

So, "mscorlib.dll" is searched for, as per the runtime probing
sequence, what you need to do is to specify FQN (Fully Qualified Name)
i.e. "System.DateTime".

I hope this might be of some help.

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 

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