MessagePack десериализирует исключение в унаследованном классе от Dictionary <,> - PullRequest
0 голосов
/ 30 апреля 2018

Привет, у меня есть класс, как:

 public class Event : Dictionary<AttributeType, object>

Тип атрибута enum

Десериализация вызовет исключение

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

Сообщение об исключении:

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

Но когда я использую этот код, все в порядке

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

Любая помощь?

Спасибо

1 Ответ

0 голосов
/ 05 мая 2018

Благодаря neuecc вот ответ

// 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 });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...