DeSearialize - class has moved from assamby A to assambly B
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: DeSearialize - class has moved from assamby A to assambly B

  1. #1
    Join Date
    Jun 2011
    Posts
    4

    Deserialize - class has moved from assamby A to assambly B

    For various reasons I moved a class (MovedClass) from assamblyA to assamblyB and also changed the namespace it was in. Everything else stayed the same.

    But now I have a problem reading the Data I serealized before moving the class

    Code:
    BinaryFormatter formatter = new BinaryFormatter();
    StayedClass Stayed = null;
    Stayed= (StayedClass)formatter.Deserialize(stream);
    Code:
    public StayedClass(SerializationInfo info, StreamingContext ctxt)
    {
         Moved = (MovedClass)info.GetValue("WebInfo", typeof(MovedClass)); // here it throws a exception
    }
    The exception says:
    Code:
    Ein Objekt muss IConvertible implementieren.
    
    which is german for:
    
    Object must implement IConvertible
    What can I do? Its essential for me to move the Classes to the new assamby. Is there any way to tell desearialize that the class has moved?
    Last edited by Gener4tor; March 4th, 2014 at 12:06 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: DeSearialize - class has moved from assamby A to assambly B

    I suspect you have more going on than moving the class. If that is all you did, wouldn't you end up with code like:

    Code:
    BinaryFormatter formatter = new BinaryFormatter();
    MovedClass moved = null;
    moved= (MovedClass)formatter.Deserialize(stream);
    As it is, you seem like you are serializing your class from one assembly and using the stream to deserialize it into a class from another assembly. The error is telling you that you need to implement the IConvertable interface (which will allow you to convert between the two classes in the different namespaces).

    Another approach to this is instead of using the binary formatter for serialization, use the XmlFormatter.

  3. #3
    Join Date
    Jun 2011
    Posts
    4

    Re: DeSearialize - class has moved from assamby A to assambly B

    I cant change to xmlFormater because I have already serialized things. I try to explain it in another way:

    * I serialized Class "StayedClass" whitch has a member called "Moved" which is of the type "AssamblyA.MovedClass" using BinaryFormatter
    * then I created a new Assambly: "AssamblyB"
    * I moved "MovedClass" to the new Assambly and changed its namespace. The type is now "AssamblyB.MovedClass"

    * After some time I try to read some files which were created in the first step. But It did not work because GetValue does not recognice that "AssamblyA.MovedClass" is the same class as "AssamblyB.MovedClass" and throws an exception

    So I need a way to tell c# that "AssamblyA.MovedClass" in now known as "AssamblyB.MovedClass"
    Last edited by Gener4tor; March 4th, 2014 at 12:37 PM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,370

    Re: DeSearialize - class has moved from assamby A to assambly B

    I got it. However, you have classes that are BINARY serialized as one type of class (class/namespace), so you can't expect them to be BINARY deserialized as a different type (class/namespace).

    Btw, the issue isn't that you've moved the class into another assembly, it's the fact that you put the class into another namespace. If you use the same namespace when you move the class, it *should* work.

    Other than keeping the namespace the same when you move the class from one assembly to the other, there is no 'telling C#' to binary deserialize to a different type.

    If you have many files saved in the older format but wish to use the newer format moving forward, you may consider writing a one-time migration step where you convert the saved files to the newer format. In other words, deserialize them into the old format, create a new object from the old and serialize them into the new format.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center