none
Why is my cast crashing? According to the VS debugger, the object seems to be in perfect shape. RRS feed

  • Question

  • Hello:

    I just discovered the remarkable technique below, it is a very simple way to implement IPC.

       http://www.codingvision.net/tips-and-tricks/c-send-data-between-processes-w-memory-mapped-file

    I literally performed a copy&paste of the 2 source codes from that website. The problem happens after the incoming Message is deserialized. First it goes into a generic object, where it can be inspected by the debugger. It looks perfect, I can see the 4 string fields.

    However, when the cast statement is reached:

    object peek = formatter.Deserialize(new MemoryStream(buffer));
    message1 = (Message) peek;

    the application crashes.

    See the Message class below.  I have two identical files ("Message.cs") in the writing app (an Excel Addin) and the reading app (a WPF app). The shared class only contains 4 strings. They are shown by the debugger.

    using System;
    
    namespace SmartQuotes
    {
        [Serializable]
        class Message
        {
            public string Description;
            public string contentsLine1;
            public string contentsLine2;
            public string contentsLine3;
        }
    }




    Saturday, July 14, 2018 2:50 PM

Answers

  • The problem is probably here: "[...] I have two identical files ("Message.cs") in the writing app and the reading app [...]". Even though the files are identical, once compiled into two applications they do not result in the same class. You are using runtime serialization, and this encodes the identity of the assembly in the serialized data. Since your two identical files are compiled inside two different assemblies, the runtime considers them different classes and the cast fails.

    One solution is to compile your class "Message" into a DLL, and then reference this DLL from the sending application and the receiving application. This will let you do the cast, because the two applications will be using the same assembly identity for the class.

    Another solution is to use a different type of serialization which doesn't embed the assembly identity. You could use, for instance, the XmlSerializer, or one of the various JSON serializers.

    • Marked as answer by Travis Banger Saturday, July 14, 2018 11:11 PM
    Saturday, July 14, 2018 4:58 PM
    Moderator
  • I think you are right, Alberto. First, I had two ASCII copies of Message.cs, next, I decided to have a single one and your recommended step goes further in that direction. When I used the debugger's "Watch" window, it refused to evaluate that variable, showing a red "X" instead. I was further informed that the class xyz is present  in both the *.exe file (my WPF app) and the *.dll (my Excel Addin).

    Saludos and gracias!   : -)


    Saturday, July 14, 2018 5:04 PM

All replies

  • See more details:

      

    Saturday, July 14, 2018 4:16 PM
  • The problem is probably here: "[...] I have two identical files ("Message.cs") in the writing app and the reading app [...]". Even though the files are identical, once compiled into two applications they do not result in the same class. You are using runtime serialization, and this encodes the identity of the assembly in the serialized data. Since your two identical files are compiled inside two different assemblies, the runtime considers them different classes and the cast fails.

    One solution is to compile your class "Message" into a DLL, and then reference this DLL from the sending application and the receiving application. This will let you do the cast, because the two applications will be using the same assembly identity for the class.

    Another solution is to use a different type of serialization which doesn't embed the assembly identity. You could use, for instance, the XmlSerializer, or one of the various JSON serializers.

    • Marked as answer by Travis Banger Saturday, July 14, 2018 11:11 PM
    Saturday, July 14, 2018 4:58 PM
    Moderator
  • I think you are right, Alberto. First, I had two ASCII copies of Message.cs, next, I decided to have a single one and your recommended step goes further in that direction. When I used the debugger's "Watch" window, it refused to evaluate that variable, showing a red "X" instead. I was further informed that the class xyz is present  in both the *.exe file (my WPF app) and the *.dll (my Excel Addin).

    Saludos and gracias!   : -)


    Saturday, July 14, 2018 5:04 PM