c# MessagePack-CSharp all public members must mark KeyAttribute or IgnoreMemberAttribute. MyType member:

641 Views Asked by At

When using MessagePack-CSharp I get the following error: c# MessagePack all public members must mark KeyAttribute or IgnoreMemberAttribute. MyTypmember:Id

The error is straightforward it throws because I didn't add a Key to some members. The reason that I didn't is because my class inherits from a 3rd party dll where those members belong to and I cannot mark them with Key attribute. I can of course can override them but that would be messy as I have dozens of models that needs to be serialized and inherit from 3rd party dll. Is there any way to specify those attributes for 3rd party assemblies similar to how automapper create profiles

1

There are 1 best solutions below

1
On

Try giving the Typeless serialize an attempt

object mc = new Sandbox.MyClass()
{
    Age = 10,
    FirstName = "hoge",
    LastName = "huga"
};

// Serialize with the typeless API
var blob = MessagePackSerializer.Typeless.Serialize(mc);

// Blob has embedded type-assembly information.
// ["Sandbox.MyClass, Sandbox",10,"hoge","huga"]
Console.WriteLine(MessagePackSerializer.ConvertToJson(bin));

// You can deserialize to MyClass again with the typeless API
// Note that no type has to be specified explicitly in the Deserialize call
// as type information is embedded in the binary blob
var objModel = MessagePackSerializer.Typeless.Deserialize(bin) as MyClass;

https://github.com/neuecc/MessagePack-CSharp#typeless