Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与protobuf-net和C的接口#_C#_.net_Protobuf Net - Fatal编程技术网

C# 与protobuf-net和C的接口#

C# 与protobuf-net和C的接口#,c#,.net,protobuf-net,C#,.net,Protobuf Net,有人知道为接口设置协议的正确方法吗 我仅使用属性得到以下异常“一旦生成序列化程序,就无法更改类型” 使用的代码: [协议] 公共课第5课测试第2课:iLesson 5测试面1 { [原成员(1)] 公共字符串名称{get;set;} [原成员(2)] 公用字符串电话{get;set;} } [原始合同] [ProtoInclude(1000,typeof(第5课测试类2))] 公共接口iLesson5测试接口1 { [原成员(1)] 字符串名称{get;set;} [原成员(2)] 字符串电话{

有人知道为接口设置协议的正确方法吗

我仅使用属性得到以下异常“一旦生成序列化程序,就无法更改类型”

使用的代码:

[协议]
公共课第5课测试第2课:iLesson 5测试面1
{
[原成员(1)]
公共字符串名称{get;set;}
[原成员(2)]
公用字符串电话{get;set;}
}
[原始合同]
[ProtoInclude(1000,typeof(第5课测试类2))]
公共接口iLesson5测试接口1
{
[原成员(1)]
字符串名称{get;set;}
[原成员(2)]
字符串电话{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
    }
}
[协议]
类包装器
{
[原成员(1)]
公共ILesson5TestInteface1内容{get;set;}
}
静态类程序
{
静态void Main()
{
包装器obj=新包装器
{
内容=新课文5TestClass2()
},克隆;
使用(var ms=new MemoryStream())
{
Serializer.Serialize(ms,obj);
ms.Position=0;
克隆=序列化程序。反序列化(毫秒);
}
//这里clone.Content*是一个Lesson5TestClass2实例
}
}

我必须看看作为根对象的接口支持是怎么回事。

为什么DTO需要接口?@jgauffin:我只是写一些代码来理解protobuf net的功能。第5课是关于接口的。:)是的,当您从未使用反序列化程序时,根对象似乎有问题。我已经编写了两个测试,一个使用包装器,另一个不使用包装器。如果首先运行带包装的测试,则不带包装的测试通过。但是如果没有包装器的测试首先运行,两个测试都会失败。:@涂鸦最好是记录一只虫子,我会调查的。非常好奇。好吧:)星期五晚上在这里,所以也许你得等到星期一(反正我会尽力的)。同时。。。我会接受你正确的答案来结束这个问题。马克,这在v2中得到了修复吗?我正在使用r480,但遇到了同样的问题。@Eric v1从未支持过接口。在v2中,他们仍然只作为成员工作。我必须解决这个问题。。。