leejw51 / protobuf-net

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

Dictionary type members do not support AsReference option #213

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
From: dshabes@hi5.com

What steps will reproduce the problem?

class AsReferenceBugTest {
    [ProtoContract]
    public class TestClass {
        [ProtoMember(1, AsReference = true)]
        public Dictionary<int, int> Data { get; set; }
    }

    public void TestMethod() {
        var model = TypeModel.Create();
        model.Add(typeof(TestClass), true);
        var obj = new TestClass { Data = new Dictionary<int, int> { { 1, 42 } } };
        TestClass restored;
        byte[] buffer;
        using (var stream = new MemoryStream()) {
            model.Serialize(stream, obj);
            buffer = stream.ToArray();
        }
        using (var stream = new MemoryStream(buffer))
            restored = (TestClass)model.Deserialize(stream, null, typeof(TestClass));
    }
}

What is the expected output?

That it will work without throwing exceptions.

What do you see instead?

Last line (deserializing) throws exception: "A reference-tracked object changed 
reference during deserialization".

What version of the product are you using?

2.0.0.431

On what operating system?

Windows 7 64-bits

Please provide any additional information below.

Seems a fairly straightforward issue: Dictionary<,> type properties (or 
IDictionary<,>) are treated by the code as IEnumerable<KeyValuePair<,>>. That 
means AsReference option value from ProtoMember attribute is passed on to be 
used to serialize items of IEnumerable. It is a problem in itself (I'd rather 
have AsReference applied to both keys and values of a dictionary) and it 
further compounded by the fact that KeyValuePair is a struct not a class hence 
above exception is thrown.

Original issue reported on code.google.com by dmitri.s...@gmail.com on 2 Aug 2011 at 6:57

GoogleCodeExporter commented 9 years ago

Original comment by marc.gravell on 3 Aug 2011 at 9:19