1

I need to do serialization and deserialization using below code. But always i am getting "Dynamic type is not a contract-type" error while serializing. I am stuck with this. I need to achieve this someway Can somebody help me on this?

using ProtoBuf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace ProtoSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                byte[] arr;
                ClassA obj = new ClassA();
                obj.ColumnList = new List<int> {1 };
                using (var stream = new MemoryStream())
                {
                    ProtoBuf.Serializer.Serialize(stream, obj);
                    arr = stream.ToArray();
                }
                using(var stream=new MemoryStream(arr)) 
                {
                    var result = Serializer.Deserialize(typeof(ClassA), stream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
    }
    [ProtoContract]
    public class ClassA 
    {
        [ProtoMember(1,DynamicType =true)]
        public IList ColumnList;
    }
}
Sankar Sai
  • 57
  • 6
  • What is the goal here? Do you want to serialize a list that can contain both 'int' and 'float' for example? – JonasH Jun 24 '20 at 14:32
  • In my case i will be having a list which will have value assigned at runtime and type will be decided at runtime..Only one type will be there in list, not multiple types...Got answer from Marc..Thanks – Sankar Sai Jul 07 '20 at 06:29

2 Answers2

1

Mostly copied from question about protobuf dynamic array.

The documentation for dynamic arrays state:

DynamicType - stores additional Type information with the type (by default it includes the AssemblyQualifiedName, although this can be controlled by the user). This makes it possible to serialize weak models, i.e. where object is used for property members, however currently this is limited to contract types (not primitives), and does not work for types with inheritance (these limitations may be removed at a later time). Like with AsReference, this uses a very different layout format

You have a primitive, therefore you can not use DynamicType. As explained in the error message you got.

A workaround could be to wrap the primitives you want to store in another type with a defined contract.

JonasH
  • 28,608
  • 2
  • 10
  • 23
1

I strongly advise against using the dynamic feature, here or elsewhere. It is deprecated in V3, probably permanently. It looks like what you really want here is something like a Value from struct.proto. This doesn't have inbuilt support in protobuf-net currently, but I'll probably add it soon. As for now: I'd probably advise using an array of a type that has those things as fields. If you make the layout look like Value does, in terms of the field numbers, it should be directly swappable later.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900