Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.01k stars 182 forks source link

Is it possible to serialize a collection with IConvertible as keys? #296

Open ngburke opened 2 months ago

ngburke commented 2 months ago

I have a code base that uses IConvertible as a way for a class to use keys of various types (ints, enums, etc) for collections. I noticed through trial and error, and from the docs that IConvertible is not supported. Is it possible in some way to enable this capability with MemoryPack? (Example code below)

[MemoryPackable]
public partial class ItemStore : Base
{
    public int MaxSpace { get; private set; }
    public int UsedSpace { get; private set; }
    public ImmutableHashSet<IConvertible> ItemsAllowed { get; private set; }

    [MemoryPackInclude]
    private int _reservedSpace;
    [MemoryPackInclude]
    private Dictionary<IConvertible, int> _storedItems;
    [MemoryPackInclude]
    private Dictionary<IConvertible, int> _reservedItems;
    [MemoryPackInclude]
    private ImmutableHashSet<IConvertible> _itemsAllowed;
    [MemoryPackInclude]
    private readonly Dictionary<IConvertible, int> _initialItemsRaw;
...