您好我有一个类:
public class Event : Dictionary<AttributeType, object>
AttributeType是枚举
反序列化将引发异常
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));
任何帮助?
由于
答案 0 :(得分:1)
感谢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 });