3

I have below class :-

[Serializable]
[DataContract(Name = "StateValueWrapper")]
public class StateValueWrapper
{
    [DataMember(Order = 1)]
    public Type StateValueType { get; set; }

    [DataMember(Order = 2)]
    public object WrappedObj { get; set; }
}

I am trying to serialize the object of above class using protobuf.net.While serializing getting an error "No suitable Default Type encoding found." please suggest me what i need to do for this? Below is my code for serilization:-

            MemoryStream ms = new MemoryStream();
            var srariazeObj = new StateValueWrapper();
            srariazeObj.StateValueType = typeof(int);
            srariazeObj.WrappedObj = 5;
            ProtoBuf.Serializer.NonGeneric.Serialize(ms, srariazeObj);
Vivek jain
  • 86
  • 3

2 Answers2

1

Type is not serializable via protobuf-net, and neither is object. I understand what you are trying to do, and if you honestly cannot know the types in advance. I suspect you should consider serialising the AssemblyQualifiedName of the type (a string) and a byte[] for the object (via MemoryStream). I can whip up an example later if you like (let me know).

However, if it is possible to state a finite lost of types you need to support (for example "string or int or Customer or Guid only") then there is a much more efficient and convenient approach - again I can whip up an example if this is your scenario - let me know.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Yes Marc i have finite set of type to be serialize using protobuf.It would be great if you share the approach which you said in last post. – Vivek jain Feb 03 '11 at 04:17
  • @vivek - I found an existing [very similar example; here](http://stackoverflow.com/questions/2678249/in-protobuf-net-how-can-i-pass-an-array-of-type-object-with-objects-of-different/2679154#2679154) – Marc Gravell Feb 03 '11 at 06:53
1

Now what i have done i have created an custom session provider and pass it to StateValueWrapper object. In side the serialize method first i serialize WrappedObj of StateValueWrapper using protobuf and assign it back to the WrappedObj now binary serializer will serialize my StateValueWrapper object which contain the type info and the byte array. While deserialization first binary serializer will deserialize the SessionStateItemCollection and return StateValueWrapper with type info and byte Array then i have done deserialization(protobuf) of WrappedObj using type info of StateValueWrapper.

Vivek jain
  • 86
  • 3