none
InvalidCastException Thrown when casting to the same type

    Question

  • I've been using Visual Studio 2010, C#.

    I've been working on this program for almost a year now. In it, I'm using information stored in separate XML files to build images. This process has worked for me hundreds of times without a problem.

    I have migrated to a new machine. The previous machine was 32 bit, the new one is 64 bit. When I open a file and attempt to deserialize, the following exception is thrown:

    System.InvalidCastException was unhandled Message=[A]System.Collections.Generic.List1[BookCreator.pageFontInfo] cannot be cast to [B]System.Collections.Generic.List1[BookCreator.pageFontInfo]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.

    Here is the code that generates the error:

            List<pageFontInfo> info; 
           
    XmlSerializer serializer = new XmlSerializer(typeof(List<pageFontInfo>)); 
           
    TextReader tr = new StreamReader(openFile); 
            info
    = (List<pageFontInfo>)serializer.Deserialize(tr); <---error thrown 

    pageFontInfo is a custom built Serializable struct. It contains two strings, a float, and a list of points. I have no idea what is going wrong. I've compared the two "originations" of the lists, and they are identical. I'm totally stumped. This has worked hundreds of times in the past before the migration.

    Edit: I just checked. I've run both the compiled .exe and source code on another machine that was 64 bit. So whatever is going wrong now does not have anything to do with that. It's specific to this installation of Visual Studio 2010.

    Tuesday, July 26, 2011 12:08 AM

Answers

  • Found it!

    After doing some research, I posted this:

    http://stackoverflow.com/questions/6825819/how-can-i-tell-when-what-is-loading-certain-assemblies

      I started stepping through my code and found that the two identical assemblies were actually different instances. I've been using the program as a class library for a web site. So in the working directory was a .dll of the project along with the .exe created when beginning the debug process.

    On load, the program was loading both the .dll, and the .exe. All I had to do was go into the working directory, and delete them both. Then the error went away.

    • Marked as answer by julio9 Tuesday, July 26, 2011 6:24 AM
    Tuesday, July 26, 2011 6:24 AM

All replies

  • Did you try Desirializing into an object. No cast, just to see if it deserializes without the cast. Put a break point after it and see if you can see anything through the debugger.

    e.g. object testSer = serializer.Deserialize(tr);

    Tuesday, July 26, 2011 12:40 AM
  • e.g. object testSer = serializer.Deserialize(tr);


    This will give you the compile Error.

    @Julio - I tried to simulate your problem in my machine but it's workign absolutely fine. I too am using VS2010 but 32 bit. Can you show us the structure 'pageFontInfo'?


    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, July 26, 2011 3:11 AM
  • do you mean the e.g. part. That's just indicates and example. Sorry, I meant to put it on the line before. Deserialize returns an object and I routinely deserialize into object.
    Tuesday, July 26, 2011 3:19 AM
  • @Adavesh - Exactly.  I've had it run fine on several machines, yet it errors out here.  Here is the struct for the pageFontInfo

     

    using System;
    using System.Collections.Generic;
    
    namespace BookCreator
    {
      [Serializable]
      public struct pageFontInfo
      {
        public string page;
        public List<System.Drawing.Point> textPosition; 
        public string fontName;
        public float fontSize;
      }
    }
    
    

     

    To get around it, should I just Deserialize to an object, then convert that to the List<struct>?  I'd prefer not to create the excess baggage.  I'm using this both as a windows application, and as a class library that will be used on a web server.

     

    Tuesday, July 26, 2011 3:49 AM
  • Also, I can deserialize to an object just fine.  When I step through the code, the object that is created is exactly what is supposed to be: a List<pageFontInfo>.  Yet, when I try to cast it back thus:

     

          object inf = (object)serializer.Deserialize(tr);
          info = (List<pageFontInfo>)inf;
    

    I get the same error.

    Tuesday, July 26, 2011 3:53 AM
  • Then there can only be one reason, the pageFontInfo you are using during deserialization is different than one you used in serialization (just a wild guess though). Are you sure that there are not two definitions of pageFontInfo structure? To be sure, please check exact type of object that is serialized and one you are casting to, both are of same type.

    And yeah, if you still find there are no issues with the types, the for time being create a new List.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, July 26, 2011 4:45 AM
  • Found it!

    After doing some research, I posted this:

    http://stackoverflow.com/questions/6825819/how-can-i-tell-when-what-is-loading-certain-assemblies

      I started stepping through my code and found that the two identical assemblies were actually different instances. I've been using the program as a class library for a web site. So in the working directory was a .dll of the project along with the .exe created when beginning the debug process.

    On load, the program was loading both the .dll, and the .exe. All I had to do was go into the working directory, and delete them both. Then the error went away.

    • Marked as answer by julio9 Tuesday, July 26, 2011 6:24 AM
    Tuesday, July 26, 2011 6:24 AM
  • Thanks for sharing your solution.

     


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, July 27, 2011 7:43 AM