Tech Off Thread

7 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Formatter.Deserialize structs

Back to Forum: Tech Off
  • User profile image
    joefra-8

    Hello,

    I have a little problem when I am trying to deserialize an Object having structs as members.

    I have a class, ClassB, that inherits from ClassA.
    ClassA have a member that is a struct.

    When I am trying to deserialize ClassB I get a MissingMemberException.

    If I move the member to ClassB and deserialize it works ok.

    Any ideas?


    The code for serializing is is:

    BinaryFormatter Formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));

    MemoryStream stream =
    new MemoryStream();
    Formatter.Serialize(stream,obj);
    stream.Position = 0;
    object clonedObj;
    clonedObj = Formatter.Deserialize(stream);
    stream.Close();
    return clonedObj;


    /Joel

  • User profile image
    Shrage

    Did you mark you struct as [Serializable] ?

  • User profile image
    joefra-8

    Yes it is marked as serializable.

  • User profile image
    Shrage

    I have tried to do the same thing, i have created a ClassA and ClassB that inherits from classA, classa has a struct class, and a memeber of that type, and classb serializes fine without a problem.

    Where did you declare the struct type? (Not the memeber of that type)  is it in ClassA? or somewhere else

  • User profile image
    joefra-8

    The struct is declared in a separate file.

  • User profile image
    Shrage

    Strange. in your code above there is a line that states:
    Formatter.Serialize(stream,obj);
    are you using obj? or this?

    EDIT: if the clone function is declared in the class you should write:
     Formatter.Serialize(stream,this);

  • User profile image
    Shrage

    Try this code: if it works then check what you have different

    using System.IO;

    using System;

    using System.Runtime.Serialization;

    using System.Runtime.Serialization.Formatters.Binary;

    [Serializable()]

    public class ClassA

    {

    public string FirstName;

    public string LastName;

    public ClassC.AddressStruct Address;

    }

    [Serializable()]

    public class ClassB : ClassA

    {

    public object Ser()

    {

    BinaryFormatter Formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));

    System.IO.MemoryStream Stream = new MemoryStream();

    Formatter.Serialize(Stream, this);

    Stream.Position = 0;

    object ReturnObj = Formatter.Deserialize(Stream);

    Stream.Close();

    return ReturnObj;

    }

    }

    public class ClassC

    {

    [Serializable()]

    public struct AddressStruct

    {

    public string Address1;

    public string Address2;

    }

    }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.