簡體   English   中英

c# MessagePack-CSharp 所有公共成員必須標記KeyAttribute 或IgnoreMemberAttribute。 我的類型成員:

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

使用 MessagePack-CSharp 時出現以下錯誤: c# MessagePack all public members must mark KeyAttribute or IgnoreMemberAttribute. MyTypmember:Id c# MessagePack all public members must mark KeyAttribute or IgnoreMemberAttribute. MyTypmember:Id

它拋出的錯誤很簡單,因為我沒有為某些成員添加密鑰。 我沒有這樣做的原因是因為我的 class 繼承自那些成員所屬的第三方 dll,我無法用 Key 屬性標記它們。 我當然可以覆蓋它們,但這會很麻煩,因為我有幾十個模型需要序列化並從第 3 方 dll 繼承。有沒有辦法為第 3 方程序集指定這些屬性,類似於 automapper 創建配置文件的方式

嘗試嘗試無類型序列化

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM