2017-06-14 69 views
0

我一直在使用MessagePackKnownCollectionItemTypeAttribute和帶嵌套字典和多態的MessagePack

[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;

這給出了同樣的錯誤。

有沒有什麼辦法可以像這樣處理嵌套字典中的抽象類? 謝謝!

回答

0

對於它的價值,我結束了使用中介類:

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

};

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