CMertens / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Using Array Conversion Functions in Properties Causes Null Exception #278

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============
dburns.2103@gmail.com

What steps will reproduce the problem?
1. Run the supplied code.
2. Notice the newDTO object does not have any value for property, Data
3.

What is the expected output? What do you see instead?
Expect the Data property to be deserialized properly.  I may be missing 
something with how ProtoBuf works.

What version of the product are you using? On what operating system?
v.2.0.0.480
Windows 7

Please provide any additional information below.

Original issue reported on code.google.com by dBurns.2...@gmail.com on 14 Mar 2012 at 8:07

Attachments:

GoogleCodeExporter commented 9 years ago
This issue can be fixed using a backing property as shown below.

[ProtoMember(1)]
private byte[] _data
{
    get;
    set;
}

public float[] Data
{
    get 
    { 
        return Array.ConvertAll<byte, float>(_data, b => Convert.ToSingle(b)*0.1f); 
    }
}

This ensures the wire data is only a byte array, but the client has access to 
the float array containing the correct values.  

Additionally, the memory stream should be seek to 0 before deserializing in the 
provider example.

    Serializer.Serialize(ms, dto);
    ms.Seek(0, SeekOrigin.Begin) // ADDED
    var newDTO = Serializer.Deserialize<DTO>(ms);
    Console.Read();

Original comment by dBurns.2...@gmail.com on 15 Mar 2012 at 6:41