与protobuf-net和C#的接口
有谁知道为接口设置ProtoContract的正确方法是什么?
我得到以下exception“仅使用属性生成序列化程序后才能更改类型 ”。
使用的代码:
[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(ms); } // here clone.Content *is* a Lesson5TestClass2 instance } }
我将不得不查看界面支持作为根对象的内容。