gra*_*fic 5 .net c# protobuf-net
有人知道为接口设置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; }
}
Run Code Online (Sandbox Code Playgroud)
只有添加以下设置,我才能反序列化:
RuntimeTypeModel.Default.Add(typeof (ILesson5TestInteface1), true)
.AddSubType(50, typeof(Lesson5TestClass2));
Run Code Online (Sandbox Code Playgroud)
我真的很想只使用属性来配置它。
我正在使用 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
}
}
Run Code Online (Sandbox Code Playgroud)
我将不得不看看作为根对象的接口支持发生了什么。