How to serialize a generic(T) object which can hold any type of data (int / string / DateTime) using Protobuf-net. Following is my code
[ProtoContract]
public class E1DataRow
{
[ProtoMember(1)]
public List<NameValue> NameValues { get; set; }
public E1DataRow()
{
NameValues = new List<NameValue>();
}
public void AddNameValue(string name, object obj, Type type)
{
NameValues.Add(new NameValue { Name = name, Value = obj, ValueType = type });
}
}
[ProtoContract]
public class NameValue
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public object Value { get; set; }
[ProtoMember(3)]
public Type ValueType { get; set; }
}
serialization code
var e1DataRows = new List<E1DataRow>();
/*
Code to add Data rows to e1DataRows
e1DataRow.AddNameValue(column.ColumnName, value, column.TypeOfColumn);
*/
using (var stream = File.OpenWrite(path))
{
Serializer.Serialize(stream, e1DataRows);
}
[ProtoMember(2, DynamicType = true)]
public object Value { get; set; }
Above code throws following error (DynamicType = true) ProtoMemberAttribute.DynamicType' is obsolete: 'Reference-tracking and dynamic-type are not currently implemented in this build; they may be reinstated later; this is partly due to doubts over whether the features are adviseable, and partly over confidence in testing all the scenarios (it takes time; that time hasn't get happened); feedback is invited'
It would be great if you can help with how to serialize a List using Protobug-net. Thanks...
public class NameValue
topublic class NameValue<T>
or use the existingKeyValuePair<TKey,TValue>
instead. – DerSchnitzNameValue<T>
is that then I can not haveList<NameValue>
with a different type (i guess).KeyValuePair<TKey, TValue>
is a good idea but would it cost me hashing operation, every time i add a new key. Basically, I am trying to convert a dataset (SqlReader) into a simple POCO class for serialization. – Ojas Maru