pizheng / protobuf-net

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

Silverlight deserialization #188

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Seems like solution SL3 from protobuf-net v2 beta r404 archive doesn't work.

It's really strange. Because I created some test scenarios to 
serialize/deserialze simple class and it's work fine with that .net3 assembly.

Here is this one:
[ProtoContract]
    public class SomeClass
    {
        [ProtoMember(1)]
        public string Name { get; set; }
        [ProtoMember(2)]
        public int Age { get; set; }

        public SomeClass()
        {
        }
    }
And wrap this into binary:

 var some = new SomeClass();
            some.Name = "Alex";
            some.Age = 22;

            byte[] packedData = null;
            using (var stream = new MemoryStream())
            {
                    Serializer.Serialize<SomeClass>(stream,some);
                    packedData = new byte[(int)stream.Length];             
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Read(packedData, 0, packedData.Length);                
            }

             using (var stream = new MemoryStream(packedData))
            {
                    var result = Serializer.Deserialize<SomeClass>(stream);  
            }

But in silverlight case deserialize method fails with "Constructor of class 
SomeClass was not found".
I thought ok. Try to use older version from protobuf-net r282.zip for SL2.0 and 
it's work fine.

Is this a bug? Also I added an assembliy to a project (vs2010) with the 
standart "add reference" dialog if that matters.

Original issue reported on code.google.com by lexw...@yandex.ru on 16 Jun 2011 at 11:17

GoogleCodeExporter commented 9 years ago
That is odd; I will investigate.

Original comment by marc.gravell on 17 Jun 2011 at 5:09

GoogleCodeExporter commented 9 years ago
I had the same issue. Below is the patch I used to fix it:
Index: Serializers/TypeSerializer.cs
===================================================================
--- Serializers/TypeSerializer.cs       (rÚvision 416)
+++ Serializers/TypeSerializer.cs       (copie de travail)
@@ -236,7 +236,7 @@
             {
                 if (!hasConstructor) TypeModel.ThrowCannotCreateInstance(constructType);
                 obj = Activator.CreateInstance(constructType
-#if !CF
+#if !CF && !SILVERLIGHT
                     , true
 #endif
                     );

As you can see, asking to include non public constructors for Silverlight build 
is the culprit and the fix is act as with CF build.

Original comment by mark.kha...@gmail.com on 20 Jun 2011 at 8:12

GoogleCodeExporter commented 9 years ago
Mark's patch is applied r418

Original comment by marc.gravell on 21 Jun 2011 at 5:15