The current time is

Tuesday, August 11, 2009

Type.GetType returning null

Writing code that deals with dynamic objects makes you eventually want to work with the types of the object themselves... So you start using "Type.GetType" and by supplying the type as a string... you get the .NET/custom TYPE itself...

And then comes one day that you're surprised that it's not working like you expected it because simply it's returning null instead of returning the correct type!!!

This is because you did not identify the type correctly/fully...

If the assembly name is specified in the typeName string you supplied, Type.GetType() will search inside this assembly only; otherwise, it tries to find one in the caller assembly and then the system assembly (mscorlib.dll).

So, to make your code work, you'll need to specify the AssemblyQualifiedName. For example:
Type.GetType("System.Drawing.Brush, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

This whole parameter (inside the GetType method) was retrieved by writing that piece of code:
typeof(System.Drawing.Brush).AssemblyQualifiedName

if you try to get the type by just writing it this way [Type.GetType("System.Drawing.Brush")], it won't work and will return the cutie null... :))

Enjoy the DYNAMICS :)))

1 comment: