【问题标题】:MessagePack with nested Dictionary's and polymorphism具有嵌套字典和多态性的 MessagePack
【发布时间】:2017-06-14 19:52:06
【问题描述】:

我一直在使用 MessagePackKnownCollectionItemTypeAttribute,例如

[MessagePackKnownCollectionItemTypeAttribute( "MyAbstractBase", typeof( MyImpl ) )] Dictionary<int, MyAbstractBase> class_member;

这很好用,但如果我有呢:

[MessagePackKnownCollectionItemTypeAttribute( "MyAbstractBase", typeof( MyImpl ) )] Dictionary<int, Dictionary<int, MyAbstractBase>> class_member;

上述方法不起作用,我得到“不支持此操作,因为无法实例化 'MyAbstractBase'。” (在运行时,当我实例化一个序列化程序时)。 我也试过 [MessagePackKnownCollectionItemTypeAttribute( "MyAbstractBase", typeof( Dictionary<int, MyImpl> ) )] Dictionary<int, Dictionary<int, MyAbstractBase>> class_member;

这给出了同样的错误。

有没有办法像这样处理嵌套字典中的抽象类? 谢谢!

【问题讨论】:

    标签: c# nested polymorphism abstract msgpack


    【解决方案1】:

    为了它的价值,我最终使用了一个中间类:

    public class MyIntermediaryDict
    {
        [MessagePackKnownCollectionItemTypeAttribute( "MyAbstractBase", typeof( MyImpl ) )]
        public Dictionary<int, MyAbstractBase> intermediary_dict;
    

    };

    class MySerializableClass
    {
        public Dictionary<int, MyIntermediaryDict> myDict;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-29
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      相关资源
      最近更新 更多