gpt4 book ai didi

c# - Protobuf net 未被告知序列化 System.Object 但仍然出现错误 : No serializer defined for type: System. 对象

转载 作者:行者123 更新时间:2023-12-05 04:13:33 25 4
gpt4 key购买 nike


System.InvalidOperationException: {"无法为以下对象准备序列化程序内部异常:{“没有为类型定义序列化程序:System.Object”}

我的代码中是否会出现其他错误?我尝试序列化大量类,但我首先测试较简单的类,以确保我的程序正常运行。


另外非常重要:SomeObject 被其他类继承(虽然我在得到错误时只是序列化这个基类)


[ProtoContract]
[ProtoInclude(10, typeof(AnotherObject))]
[ProtoInclude(11, typeof(YetAgainObject))]
public class SomeObject {
[ProtoMember(1)]
String mName = "";
[ProtoMember(2)]
String mOwner = "";
[ProtoMember(3)]
String mDesc = "";
}

[ProtoContract]
public class AnotherObject : SomeObject{
[ProtoMember(1)]
public object[] someList{ get; set: }
}

....

最佳答案

在序列化原始对象的过程中,它检查是否所有 ProtoInclude 语句。它还检查正确的序列化结构和质量!如果其中任何一个有错误,它会在您尝试序列化的那个中抛出一个错误,但它不会告诉您它来自哪个,而是说您继承的类导致了错误。

因此,在处理大量类而不单独检查它们时,需要谨慎。除了没有为序列化定义对象之外的其他错误也是如此。

因此我的错误是在 AnotherObject 中,具体的解决方法是:

 [ProtoContract]
public class AnotherObject : SomeObject{
[ProtoMember(1, DynamicType = true)]
public object[] someList{ get; set: }
}

因为你必须告诉它对象包含可序列化的东西(它会信任你)。

关于c# - Protobuf net 未被告知序列化 System.Object 但仍然出现错误 : No serializer defined for type: System. 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com