Type.GetType() behavior on Compact Framework

  • Thread starter Thread starter Antao Almada
  • Start date Start date
A

Antao Almada

I was having some problems with the Type.GetType() method and did some
tests. I called the method several times in this order:

// using type name only
Type t1 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest"); //
fails

// using type name and assembly name
Type t2 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest,
YDreams.CF.iGarment.Messaging"); // fails

// explicitly load the assembly
Assembly test = Assembly.LoadFrom("YDreams.CF.iGarment.Messaging.dll");

// using type name only
Type t3 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest"); //
fails

// using type name and assembly name
Type t4 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest,
YDreams.CF.iGarment.Messaging"); // succeeds

Conclusion:
It only succeeds after the assembly is explicitly loaded and using both
the type name and assembly name. Wierd...

Just in case anyone has the same problem...

Antao
____________________________
Antao Almada
http://www.ydreams.com/
 
Antao Almada said:
I was having some problems with the Type.GetType() method and did some
tests. I called the method several times in this order:

// using type name only
Type t1 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest"); //
fails

// using type name and assembly name
Type t2 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest,
YDreams.CF.iGarment.Messaging"); // fails

// explicitly load the assembly
Assembly test = Assembly.LoadFrom("YDreams.CF.iGarment.Messaging.dll");

// using type name only
Type t3 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest"); //
fails

// using type name and assembly name
Type t4 = Type.GetType("YDreams.iGarment.Messaging.MapListRequest,
YDreams.CF.iGarment.Messaging"); // succeeds

Conclusion:
It only succeeds after the assembly is explicitly loaded and using both
the type name and assembly name. Wierd...

Just in case anyone has the same problem...

I don't think it's that weird, actually - I think it's exactly the
documented behaviour.

If you give the assembly name, it will look through the already loaded
assemblies, but won't load the assembly itself unless you give it the
*full* assembly name (with version numbers etc). If you don't give it
the assembly name at all, it only looks in mscorlib and the current
assembly.
 
I called it "weird" because I never has this problem on the regular
framework.

Also, because my application has a reference to the assembly, and VS
automatically transfers it to the device. It should have been loaded
automatically.

Just wanted everybody to know, in case someone had the same problem....

Antao
____________________________
Antao Almada
http://www.ydreams.com/
 
Antao Almada said:
I called it "weird" because I never has this problem on the regular
framework.

Also, because my application has a reference to the assembly, and VS
automatically transfers it to the device. It should have been loaded
automatically.

Assemblies aren't usually loaded until they're first used. Certainly
they aren't loaded just because there's a reference in VS.NET. (They're
deployed due to that reference, but not loaded until they're used.)

It looks like Type.GetType, when given a simple assembly name on the
desktop, looks for that assembly at least in the current directory, and
tries to load it. I don't think that's guaranteed behaviour, however -
the docs are somewhat ambiguous on it.

Personally, if I know that a type is in an assembly other than the
calling one, I make sure it's loaded and then use Assembly.GetType
instead.
 
Yes, I run across this when writing binary serialization code. I ended up
writing a function that would first attempt to get type via Type.GetType()
and if failed, parse the type name string and load the assembly explicitly,
then enumerate assembly types.
 
Back
Top