Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable Default Type encoding found. While serialize using protobuf

Tags:

protobuf-net

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);
like image 412
Vivek jain Avatar asked Dec 30 '25 07:12

Vivek jain


1 Answers

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.

like image 169
Marc Gravell Avatar answered Jan 02 '26 14:01

Marc Gravell