使用protobuf-net时"No serializer defined for type: System.Collections.HashTable"或"System.ComponentModel



我在尝试在protobuf-net中序列化类时遇到了以下错误。 哈希表包含许多不同类型的对象数组,所以不幸的是我不能使用字典。 ISite 有派生类型,但如果接口本身是 .NET 的一部分,那么据我所知,我能做的不多。

我已经查找了一些关于RuntimeTypeModel的东西,但据我所知,它仅适用于您创建的自定义类。任何指导将不胜感激!谢谢。

Hashtable不能很好地工作的原因是它不知道你要在其中放什么。 Protobuf-net是一个基于合约的序列化程序;Hashtable本质上是一张objectobject的地图。object不是合同——事实上,它与合同完全相反。相比之下,Dictionary<int, SomeType>工作得很好(只要SomeType可以用作合同)。

ISite不起作用的原因是,大概它是一个接口。接口表示实现,而不是数据。碰巧的是,protobuf-net确实对接口成员有一些有限的支持,但坦率地说,你最好让接口远离DTO(即你的数据契约)。它应该创建什么类型的ISite实现?为此需要哪些数据?您所使用的SomeFunSite : ISite如何实例化并连接到事物?序列化程序要参与的问题太多。序列化程序想要的是:"我正在序列化Foo;Foo有 2 个整数、一个字符串和一个Bar- 尽管在某些情况下还需要了解SuperBar : Bar子类"。这已经绰绰有余了。

良好的合同:

[ProtoContract]
public class MyData {
[ProtoMember(1)]
public Dictionary<int, SomeType> Items {get; } = new Dictionary<int, SomeType>();
[ProtoMember(2)]
public SomethingElse Whatever { get;set;}
}

不良合同:

[ProtoContract]
public class MyData {
[ProtoMember(1)]
public Hashtable Items {get; } = new Hashtable();
[ProtoMember(2)]
public ISometing Whatever { get;set;}
}

在某些情况下,可能需要配置RuntimeTypeModel以了解您要执行的操作,但是:并非总是如此。这将取决于上下文;我没有的背景。


编辑:小澄清:源代码现在支持仅{get;}属性,但在当前的NuGet版本中不支持 - 基本上,暂时不要使用它!


下面是标记存储类似数据的可运行示例:

using ProtoBuf;
using System;
using System.Collections.Generic;
static class Program
{
static void Main()
{
var obj = new MyData
{
Site = new BasicSite { BaseHost = "http://somesite.org" },
Items =
{
{"key 1", SomeType.Create(123) },
{"key 2", SomeType.Create("abc") },
{"key 3", SomeType.Create(new Whatever { Id = 456, Name = "def" }) },
}
};
var clone = Serializer.DeepClone(obj);
Console.WriteLine($"Site: {clone.Site}");
foreach(var pair in clone.Items)
{
Console.WriteLine($"{pair.Key} = {pair.Value}");
}
}
}

[ProtoContract]
class MyData
{
private readonly Dictionary<string, SomeType> _items
= new Dictionary<string, SomeType>();
[ProtoMember(1)]
public Dictionary<string, SomeType> Items => _items;
[ProtoMember(2)]
public ISite Site { get; set; }
}
[ProtoContract]
[ProtoInclude(1, typeof(SomeType<int>))]
[ProtoInclude(2, typeof(SomeType<string>))]
[ProtoInclude(3, typeof(SomeType<Whatever>))]
abstract class SomeType
{
public object Value { get { return UntypedValue; } set { UntypedValue = value; } }
protected abstract object UntypedValue { get; set; }
public static SomeType<T> Create<T>(T value) => new SomeType<T> { Value = value };
}
[ProtoContract]
class SomeType<T> : SomeType
{
[ProtoMember(1)]
public new T Value { get; set; }
protected override object UntypedValue { get => Value; set => Value = (T)value; }
public override string ToString() => Value?.ToString() ?? "";
}
[ProtoContract]
class Whatever
{
[ProtoMember(1)]
public int Id { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
public override string ToString() => $"{Id}, {Name}";
}
[ProtoContract]
[ProtoInclude(1, typeof(BasicSite))]
interface ISite
{
void SomeMethod();
}
[ProtoContract]
class BasicSite : ISite
{
void ISite.SomeMethod() { Console.WriteLine(BaseHost); }
[ProtoMember(1)]
public string BaseHost { get; set; }
public override string ToString() => BaseHost;
}

相关内容

最新更新