• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

XmlSerializer BIZARRE-NESS! Very strange behavior!

I have this code:
 
// Create and deserialize a ViewletMetabase object..
XmlSerializer serializer = new XmlSerializer(typeof(ViewletMetabase));
MemoryStream ms = new MemoryStream(File.ReadAllBytes(MetabaseFileName));
Object obj serializer.Deserialize(ms);
// At this point, obj is NOT null

ViewletMetabase vm = obj as ViewletMetabase;
// At this point vm is null!?!?!?  WTF?

Open in new window


If I use reflection, I can interact with obj as a ViewletMetabase - but it will NOT cast to a ViewletMetabase.  

'obj is ViewletMetabase' returns false.

How is this possible?

As a little bit of background.  This code is running inside of the VisualStudio 2010 process itself, as part of a UITypeEditor - but I can't for the life of me understand how this could actually be relevant.  Regardless of where this code is running, how can I pass typeof(ViewletMetabase) into an XmlSerializer object, deserialize a stream, and then get back an object that is not of type ViewletMetabase 3 lines later within the same method?

Any suggestions greatly appreciated.
0
kalliopi
Asked:
kalliopi
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
Does a direct cast work or throw an exception?
ViewletMetabase vm = (ViewletMetabase)obj;

Open in new window

0
 
kalliopiAuthor Commented:
Throws an exception.
0
 
wdosanjosCommented:
What are the values for obj.GetType().Name and obj.GetType().Namespace?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kalliopiAuthor Commented:
They are the exact same name and namespace as my object:
Fully.Qualified.Namespace.ViewletMetabase

Everything matches exactly (including the ability to interact with the object through reflection) exactly as though it is a viewlet metabase.  It just won't cast to ViewletMetbase.  It's as though there are two different "copies" of the type or something - same name, same namespace, same everything - but different "types".

It's as though the dll that my code is getting compiled into is being loaded twice.  And Deserialize uses one copy of the type, but my function uses the other and therefore the system can't "translate" between the two types because they are not actually the same type.

Does that make any sense?  I'm totally baffled, but just the fact that this happens seems to me to be a bug in VS...

Totally confused.
0
 
wdosanjosCommented:
Please post the exception (complete message and stack trace) that occurred when you applied @kaufmed's suggestion.
0
 
wdosanjosCommented:
@kalliopi I think you are on the right track.  Here is what I found about loading an assembly into multiple contexts.

Loading an assembly into multiple contexts can cause type identity problems. If the same type is loaded from the same assembly into two different contexts, it is as if two different types with the same name had been loaded. An InvalidCastException is thrown if you try to cast one type to the other, with the confusing message that type MyType cannot be cast to type MyType.

http://msdn.microsoft.com/en-us/library/dd153782.aspx#avoid_loading_into_multiple_contexts

You can verify the loaded assemblies in your code by adding the following lines just before 'ViewletMetabase vm = obj as ViewletMetabase;'

var loadedAssemblies = string.Empty;
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies().OrderBy(a => a.CodeBase))
	loadedAssemblies += string.Format("HostContext: {0}; CodeBase: {1}\n", asm.HostContext, asm.CodeBase);
// loadedAssemblies.ToString() contains a list of all loaded assemblies.
// Check if your assembly is listed twice under different HostContext's

Open in new window

0
 
kalliopiAuthor Commented:
I have not been able to validate this - but I like the approach as a way to determine if my type IS actually being loaded twice (as it appears to be).  I don't know exactly what the solution will be if that IS happening - but at least I'll have an explanation for the behavior.  Thanks.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now