DerrickBrayanClayton / protobuf-net

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

unable to cast exception with nullable properties #93

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Unable to cast object of type 'ProtoBuf.Property.PropertyInt32Variant`1

[MySpace.Comments.Framework.CommentPermissions]' to type 
'ProtoBuf.Property.Property`2

[MySpace.Comments.Framework.CommentPermissions,MySpace.Comments.Framework.C
ommentPermissions]'.

I get the following exception on propertyFactory method "Property<T,T> 
CreatePassThru<T>(int tag, ref DataFormat format)", line :" Property<T,T> 
prop = (Property<T, T>)CreateProperty<T>(typeof(T), ref format, 
MemberSerializationOptions.None);" when it tries to initialize a nullable 
property 

Original issue reported on code.google.com by sangeeta...@gmail.com on 16 Feb 2010 at 3:27

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
looks like nullable enums cannot be serialized, it still throws the cast 
exception with  
the original code

Original comment by sangeeta...@gmail.com on 17 Feb 2010 at 1:25

GoogleCodeExporter commented 8 years ago
I'll investigate; thanks.

Original comment by marc.gravell on 17 Feb 2010 at 7:48

GoogleCodeExporter commented 8 years ago
I have a fix for this issue.Let me know if its good enough. Basically I fork 
off 
creating a new "PropertyNullableEnum" serializer for it, that serializes a byte 
at the 
beginning indicating if the enum has value or not and then serializing the 
actual value 
as int. 

Original comment by sangeeta...@gmail.com on 23 Feb 2010 at 1:07

Attachments:

GoogleCodeExporter commented 8 years ago
I'll take a look, but adding an extra byte sounds like it is a breaking change. 
I'm 
sorry I haven't managed to get to this yet, but I've swamped with other work. 
I've 
bumped this up my list.

Original comment by marc.gravell on 23 Feb 2010 at 5:18

GoogleCodeExporter commented 8 years ago
I finally got some time to look at this, but cannot reproduce (see below). What 
do I 
need to reproduce this?

using System;
using ProtoBuf;
[ProtoContract]
class Data
{
    public enum Foo { A, B, C }
    [ProtoMember(1)]
    public Foo? Bar { get; set; }
    static void Main()
    {
        Data data = new Data();
        data.Bar = Foo.A;
        data = Serializer.DeepClone(data);
        Console.WriteLine(data.Bar);
        data.Bar = null;
        data = Serializer.DeepClone(data);
        Console.WriteLine(data.Bar);
        data.Bar = Foo.B;
        data = Serializer.DeepClone(data);
        Console.WriteLine(data.Bar);
    }
}

Original comment by marc.gravell on 5 Mar 2010 at 6:07