开发者

No suitable Default Type encoding found. While serialize using protobuf

I have below class :-

[Serializable]
[DataContract(Name = "StateValueWrapper")]
public class StateValueWrapper
{
    [DataMember(Order = 1)]
    public Type StateValueTyp开发者_如何学Goe { 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);


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜