Как сериализовать словарь объектов с помощью пакета сообщенийC#

Место общения программистов C#
Anonymous
Как сериализовать словарь объектов с помощью пакета сообщений

Сообщение Anonymous »

В идеале я рассматриваю возможность сериализации любого класса в двоичный файл. Поскольку старый добрый BinaryFormatter устарел, я изучаю MessagePack, однако, похоже, есть случаи, с которыми он борется.
public class Foo
{
public Dictionary Bar { get; set; }
}

var foo = new Foo { Bar = new Dictionary() };
foo.Bar.Add("binding", JObject.Parse(@"{
""source"": ""EventRegistrationFormDataSource"",
""inputs"": {
""sourceType"": {
""value"": ""msevtmgt_event""
}
},
""outputPath"": ""msevtmgt_description"",
""outputType"": ""memo""
}"));

var options = TypelessContractlessStandardResolver.Options;
MessagePackSerializer.Serialize(foo, options);

Ошибка приведена ниже. Я играл с разными комбинациями преобразователей, однако из-за этой ошибки, похоже, все не удалось. Есть идеи?
System.ArgumentException: GenericArguments[0], 'Newtonsoft.Json.Linq.JContainer', on 'MessagePack.Formatters.NonGenericListFormatter`1[T]' violates the constraint of type 'T'.

Stack Trace: 
RuntimeTypeHandle.Instantiate(RuntimeType inst)
RuntimeType.MakeGenericType(Type[] instantiation)
--- End of inner exception stack trace ---
RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
RuntimeType.MakeGenericType(Type[] instantiation)
DynamicGenericResolverGetFormatterHelper.GetFormatter(Type t)
FormatterCache`1.cctor()
--- End of stack trace from previous location ---
FormatterResolverExtensions.Throw(TypeInitializationException ex)
Serialize(Byte[][], Object[], MessagePackWriter&, JProperty, MessagePackSerializerOptions)

DictionaryFormatterBase`5.Serialize(MessagePackWriter& writer, TDictionary value, MessagePackSerializerOptions options)
Serialize(Byte[][], Object[], MessagePackWriter&, Foo, MessagePackSerializerOptions)
AnonymousSerializableFormatter`1.Serialize(MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)


Подробнее здесь: https://stackoverflow.com/questions/790 ... ssage-pack

Вернуться в «C#»