2

Hi I have a class like :

 public class Event : Dictionary<AttributeType, object>

AttributeType is enum

Deserializing will throw an exception

var @event = new Event { { AttributeType.EventId, Guid.NewGuid() } };
MessagePackSerializer.Deserialize<Event(MessagePackSerializer.Serialize(@event));

Exception message is:

System.ArgumentException: 'The value "21" is not of type "BRCo.Core.Common.Enums.AttributeType" and cannot be used in this generic collection.'

But when I use this code it's ok

var @event = new Event { { AttributeType.EventId, Guid.NewGuid() } };
MessagePackSerializer.Deserialize<Dictionary<AttributeType, object>>(MessagePackSerializer.Serialize(@event));

Any help?

Thanks

Hesam Faridmehr
  • 1,176
  • 8
  • 20

1 Answers1

3

Thanks to neuecc here is the answer

// create custom dictionary inherited formatter.
public class EventFormatter :DictionaryFormatterBase<AttributeType,object, Event>
{
   protected override Event Create(int count)
   {
       return new Event();
   }

   protected override void Add(Event collection, int index, AttributeType key, object value)
   {
       collection.Add(key, value);
   }
}

// and register it.
MessagePack.Resolvers.CompositeResolver.RegisterAndSetAsDefault(
new[] { new EventFormatter() },
new[] { StandardResolver.Instance });
Hesam Faridmehr
  • 1,176
  • 8
  • 20