473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

System.GetType returns null

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

Sep 30 '05 #1
4 11970
audipen <au*****@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 30 '05 #2
Thanks for the clarification...Will be carefull the next time:)

Sep 30 '05 #3
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.
Sep 30 '05 #4
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

Oct 24 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Suresh | last post by:
Hi, I have an C# CSEXE.exe (CSexe.cs) and a CSDll.dll (CSdll.cs). exe is compiled with a reference to dll. Calling for the class Type defined in CSDLL.dll using Type.GetType(...
2
by: JohnnySparkles | last post by:
Hi everyone, I'm currently writing an application which uses the XmlSerializer class to serialize/deserialize objects to/from xml. Now when deserializing an XmlDocument back into the object,...
1
by: hazz | last post by:
"Value cannot be null.\r\nParameter name: type" is the exception thrown after the CreateInstance method below. Type t = Type.GetType(GetConfigValue("PasswordProvider")); IPasswordProvider ppdr=...
1
by: audipen | last post by:
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 =...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
1
by: Jamey McElveen | last post by:
I have the typename of a form I wish to to load dynamically (example:MyCompany.Client.Win.Security.Detail.SysUserDetailForm). However, when I call Type.GetType(typeName) it return null I am...
1
by: =?Utf-8?B?QnJpYW4gQ29iYg==?= | last post by:
This code is contained in one source file in VS 2005 project: using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace ReflectionTest {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.