简体   繁体   中英

Serialize nested objects in C#

I got 2 user-defined classes, called Event and Image , Event has a property stored a list of EventImage , called EventImages . And in Image class, there's a byte[] type property which store the byte[] of one Image file.

Here's the definitions of the 2 classes :

[Serializable]
public Class Event
{
    public String Name {get; set;}
    public DateTime EventTime { get; set;}
    public List<Image> EventImages { get; set; }
    ...
}

[Serializable]
public Class Image
{
    public DateTime ImageTime { get; set;}
    public byte[] pData {get; set;}
    ...
}

Now my question is, I want to serialize my Event object to byte[] , and I expect that the whole content of it will be serialize , too, but it seems that I failed.

Here's my code to do Serialization :

    public static byte[] ObjectToByteArray(object obj)
    {
        if (obj == null)
        {
            return null;
        }
        else
        {
            BinaryFormatter bF = new BinaryFormatter();
            using (MemoryStream mS = new MemoryStream())
            {
                bF.Serialize(mS, obj);
                return mS.ToArray();
            }
        }
    }

and here's the code for verification :

Console.WriteLine(ObjectToByteArray(Event));
Console.WriteLine(ObjectToByteArray(Event.EventImage));        
Console.WriteLine(ObjectToByteArray(Event.EventImages.FirstOrDefault().pData));

and the results are(just assumption value) : 100 200 300

But I expect the result should be 600(100+200+300), 500(200+300) and 300.

So, I think my serialization doesn't really serialize the whole content, it just serialize properties with Basic Types, but without the nested objects, am I right?

I've searched lots of posts, and I found plenty of answers to similar questions mentioned "XML Serialization", but I'm not sure whether its helpful or not. Need I use that or is there any other better way? Thanks in advance!

Quickly made a test to check equality. It passes.

Serializes and deserializes correctly.

Conclusion, don't judge whether something is happening or not until you've tested it.

    public static void Run()
    {
        var i = new Image
        {
            ImageTime = DateTime.UtcNow,
            pData = Guid.NewGuid().ToByteArray()
        };

        var e = new Event
        {
            Name = Guid.NewGuid().ToString(),
            EventTime = DateTime.UtcNow,
            EventImages = new List<Image> {i}
        };

        var bytes = ObjectToByteArray(e);
        var e2 = ObjectFromByteArray(bytes);

        Console.WriteLine(e.Equals(e2));
    }

    public static byte[] ObjectToByteArray(object obj)
    {
        if (obj == null)
        {
            return null;
        }

        var bF = new BinaryFormatter();
        using (var mS = new MemoryStream())
        {
            bF.Serialize(mS, obj);
            return mS.ToArray();
        }
    }

    public static object ObjectFromByteArray(byte[] bytes)
    {
        if (bytes == null)
        {
            return null;
        }

        var bF = new BinaryFormatter();
        using (var mS = new MemoryStream(bytes))
        {
            return bF.Deserialize(mS);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM