denghongcai / protobuf-net

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

"Unable to cast object" error when no casting should be needed #256

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This code crashes with "Unable to cast object of type Derived1 to type 
Derived2" but it seems like there's nothing wrong with it.

    [ProtoContract]
    interface IBase { }
    [ProtoContract]
    class Derived1 : IBase { }
    [ProtoContract]
    class Derived2 : IBase { }

    [ProtoContract]
    class Container
    {
        public Container()
        {
            Object = new Derived1();
        }

        [ProtoMember(1, IsRequired = true)]
        public IBase Object;
    }

    class Test
    {
        private static byte[] Serialize(object obj)
        {
            var stream = new MemoryStream();
            Serializer.Serialize(stream, obj);
            return stream.ToArray();
        }

        private static T Deserialize<T>(byte[] serializedData)
        {
            var stream = new MemoryStream(serializedData);
            return Serializer.Deserialize<T>(stream);
        }

        public static void Run()
        {
            var model = RuntimeTypeModel.Default;
            var baseModel = model.Add(typeof(IBase), true);
            baseModel.AddSubType(150, model.Add(typeof(Derived1), true).Type);
            baseModel.AddSubType(151, model.Add(typeof(Derived2), true).Type);

            var container = new Container();
            container.Object = new Derived2();
            var data = Serialize(container);
            Deserialize<Container>(data);
        }
    }

Note: Same thing happens when using the DynamicType mechanism rather than the 
AddSubType one.

Original issue reported on code.google.com by logix...@gmail.com on 2 Dec 2011 at 11:17

GoogleCodeExporter commented 9 years ago
got a similar issue.

The line that seems to cause the problem is:

Object = new Derived1();

if you don't initialize the member, it works fine.

Original comment by sebastia...@googlemail.com on 5 Jan 2012 at 2:47

GoogleCodeExporter commented 9 years ago
I am having similar issues with casting across siblings in general.  I'll try 
the fix in comment 1 and see if I can't get a workaround going.

It does seem to be based upon object state/initialization, and is proving 
really hard to track down.

In my VM everything works, casts and transports correctly.  When hooked up to 
hardware, it fails casts.

Original comment by mikepe...@gmail.com on 10 Feb 2012 at 7:17

GoogleCodeExporter commented 9 years ago
ok, delved a bit deeperi nto the code and one of my colleagues coded a number 
of items that looked like what causes this:

http://stackoverflow.com/questions/6916294/cast-sometimes-throws-exception-but-a
s-followed-by-dereference-does-not

My problem might not necessarily be the same, and while we're not serializing 
any of those properties, they may be why the cast is failing. 

Original comment by mikepe...@gmail.com on 10 Feb 2012 at 7:44

GoogleCodeExporter commented 9 years ago
Also got a similar issue after migrating from v1 to v2

Original comment by ghu...@gmail.com on 10 Sep 2012 at 2:29