views:

96

answers:

4

hi

I am created a post before " Object to byte not working " . I fixed problems that users said me , but there is still problem .

Error Message : The constructor to deserialize an object of type 'WindowsFormsApplication1.Form1+Item' was not found.;

void start()
{

 Item item = new Item();
 item.files.Add(@"test");
 byte[] b = ObjectToByteArray(item);  
     Item k = Desriles(b);

}


[Serializable]
public class Item : ISerializable
{
    public Item()
    {
        files = new List<string>();
        Exclude = false;
        CleanEmptyFolder = false;
    }
    public List<string> files;
    public string MusicProfileName;
    public bool Exclude;

    #region ISerializable Members

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("files", files);
        info.AddValue("MusicProfileName", MusicProfileName);
        info.AddValue("Exclude", Exclude);
    }

    #endregion
}

public byte[] ObjectToByteArray(object _Object)
{
    using (var stream = new MemoryStream())
    {
        // serialize object 
        var formatter = new BinaryFormatter();
        formatter.Serialize(stream, _Object);

        // get a byte array
        var bytes = new byte[stream.Length];
        using (BinaryReader br = new BinaryReader(stream))
        {
            bytes = br.ReadBytes(Convert.ToInt32(stream.Length));
        }

        return bytes;
    }

}


    public Item Desriles(byte[] items)
    {
        using (MemoryStream stream = new MemoryStream())
        {

            stream.SetLength(items.LongLength);
            stream.write(items, 0, items.Length);
            var formatter = new BinaryFormatter();
            stream.Seek(0, SeekOrigin.Begin);

            object item = formatter.Deserialize(stream); // Here I will get error
            return (Item)item;


        }
    }
+2  A: 

In this section:

using (MemoryStream stream = new MemoryStream())
{
    stream.SetLength(items.LongLength);
    stream.Read(items, 0, items.Length);
    [...]
    object item = formatter.Deserialize(stream);

it seems you are creating a new, empty memory stream, then attempting to read from it, and Deserialize from it.

Of course it fails. The stream is empty.

abelenky
thanks , I updated,, but I still get error : The constructor to deserialize an object of type 'WindowsFormsApplication1.Form1+Item' was not found .
pedram
+1  A: 

The serialization code can't work properly, you forgot to reset the stream back to the beginning. The better mouse trap:

    public byte[] ObjectToByteArray(object _Object) {
        using (var stream = new MemoryStream()) {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, _Object);
            return stream.ToArray();
        }
    }

The deserialization code can similarly be simplified:

    public Item Desriles(byte[] items) {
        using (MemoryStream stream = new MemoryStream(items)) {
            var formatter = new BinaryFormatter();
            return (Item)formatter.Deserialize(stream);
        }
    }

And you don't need GetObjectData().

Hans Passant
I still Get That Error : The constructor to deserialize an object of type 'WindowsFormsApplication1.Form1+Item' was not found .
pedram
A: 

abelenky makes a good point, but I don't think your:

public byte[] ObjectToByteArray(object _Object)

works either.

JohnB
A: 

Thanks to All :

I found problem : I should base that to ISerializable

pedram