[英]Interfaces with protobuf-net and C#
有誰知道為接口設置ProtoContract的正確方法是什么?
我得到以下異常“僅使用屬性生成序列化程序后才能更改類型 ”。
使用的代碼:
[ProtoContract]
public class Lesson5TestClass2 : ILesson5TestInteface1
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public string Phone { get; set; }
}
[ProtoContract]
[ProtoInclude(1000, typeof(Lesson5TestClass2))]
public interface ILesson5TestInteface1
{
[ProtoMember(1)]
string Name { get; set; }
[ProtoMember(2)]
string Phone { get; set; }
}
只有添加以下設置,我才能反序列化:
RuntimeTypeModel.Default.Add(typeof (ILesson5TestInteface1), true)
.AddSubType(50, typeof(Lesson5TestClass2));
我真的很想只使用屬性來配置它。
我正在使用NuGet的protobuf-net r470。
順便說一句:這個例子來自一組“通過測試的教訓”,展示了如何使用protobuf-net為我的同事進行序列化。
謝謝閱讀 :)
有趣; 是的,看起來有些東西在那里。 但是,它作為成員公開時確實有效,即
[ProtoContract]
class Wrapper
{
[ProtoMember(1)]
public ILesson5TestInteface1 Content { get; set; }
}
static class Program
{
static void Main()
{
Wrapper obj = new Wrapper
{
Content = new Lesson5TestClass2()
}, clone;
using(var ms = new MemoryStream())
{
Serializer.Serialize(ms, obj);
ms.Position = 0;
clone = Serializer.Deserialize<Wrapper>(ms);
}
// here clone.Content *is* a Lesson5TestClass2 instance
}
}
我將不得不查看界面支持作為根對象的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.