找不到合适的默认类型编码。使用 protobuf 进行序列化时
我有以下类:-
[Serializable]
[DataContract(Name = "StateValueWrapper")]
public class StateValueWrapper
{
[DataMember(Order = 1)]
public Type StateValueType { get; set; }
[DataMember(Order = 2)]
public object WrappedObj { get; set; }
}
我正在尝试使用 protobuf.net 序列化上述类的对象。序列化时出现错误“找不到合适的默认类型编码”。请建议我为此需要做什么?下面是我的序列化代码:-
MemoryStream ms = new MemoryStream();
var srariazeObj = new StateValueWrapper();
srariazeObj.StateValueType = typeof(int);
srariazeObj.WrappedObj = 5;
ProtoBuf.Serializer.NonGeneric.Serialize(ms, srariazeObj);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Type
不能通过 protobuf-net 序列化,object
也不能通过 protobuf-net 序列化。我明白你想要做什么,如果你真的无法提前知道类型。我怀疑您应该考虑序列化类型的AssemblyQualifiedName
(string
)和对象的byte[]
(通过MemoryStream )。如果您愿意的话,我可以稍后再举一个例子(请告诉我)。
但是,如果可以声明您需要支持的有限类型丢失(例如“string或int或Customer或Guidonly”),那么就有很多更有效和方便的方法 - 如果这是您的情况,我可以再举一个例子 - 让我知道。
Type
is not serializable via protobuf-net, and neither isobject
. I understand what you are trying to do, and if you honestly cannot know the types in advance. I suspect you should consider serialising theAssemblyQualifiedName
of the type (astring
) and abyte[]
for the object (viaMemoryStream
). 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.
现在我所做的就是创建一个自定义会话提供程序并将其传递给 StateValueWrapper 对象。在序列化方法中,我首先使用 protobuf 序列化 StateValueWrapper 的 WrappedObj 并将其分配回 WrappedObj 现在二进制序列化器将序列化我的 StateValueWrapper 对象,其中包含类型信息和字节数组。虽然反序列化第一个二进制序列化器将反序列化 SessionStateItemCollection 并返回带有类型信息和字节数组的 StateValueWrapper,然后我已经使用 StateValueWrapper 的类型信息完成了 WrappedObj 的反序列化(protobuf)。
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.