//设置扩展对象
ProtoBuf.Extensible.AppendValue
//读取扩展对象
ProtoBuf.Extensible.GetValue

最近通过C#的TcpClient调用java服务器接口,使用了protobuf的协议,在调试接口的时候一直取不到extensionObject

Response.cs如下

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------ // Generated from: proto/Response.proto
namespace hrv
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Response")]
public partial class Response : global::ProtoBuf.IExtensible
{
public Response() {} private hrv.Response.Status _status;
[global::ProtoBuf.ProtoMember(, IsRequired = true, Name=@"status", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public hrv.Response.Status status
{
get { return _status; }
set { _status = value; }
} private string _timestamp = "";
[global::ProtoBuf.ProtoMember(, IsRequired = false, Name=@"timestamp", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue("")]
public string timestamp
{
get { return _timestamp; }
set { _timestamp = value; }
} private hrv.RespFailed _respfailed = null;
[global::ProtoBuf.ProtoMember(, IsRequired = false, Name=@"respfailed", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public hrv.RespFailed respfailed
{
get { return _respfailed; }
set { _respfailed = value; }
} private hrv.RespSuccess _respSuccess = null;
[global::ProtoBuf.ProtoMember(, IsRequired = false, Name=@"respSuccess", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(null)]
public hrv.RespSuccess respSuccess
{
get { return _respSuccess; }
set { _respSuccess = value; }
}
[global::ProtoBuf.ProtoContract(Name=@"Status")]
public enum Status
{ [global::ProtoBuf.ProtoEnum(Name=@"OK", Value=)]
OK = , [global::ProtoBuf.ProtoEnum(Name=@"ERROR", Value=)]
ERROR =
} private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
} [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"RespFailed")]
public partial class RespFailed : global::ProtoBuf.IExtensible
{
public RespFailed() {} private int _code;
[global::ProtoBuf.ProtoMember(, IsRequired = true, Name=@"code", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public int code
{
get { return _code; }
set { _code = value; }
}
private string _error;
[global::ProtoBuf.ProtoMember(, IsRequired = true, Name=@"error", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string error
{
get { return _error; }
set { _error = value; }
}
private string _errorDescription;
[global::ProtoBuf.ProtoMember(, IsRequired = true, Name=@"errorDescription", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string errorDescription
{
get { return _errorDescription; }
set { _errorDescription = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
} [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"RespSuccess")]
public partial class RespSuccess : global::ProtoBuf.IExtensible
{
public RespSuccess() {} private hrv.RespSuccess.Type _type;
[global::ProtoBuf.ProtoMember(, IsRequired = true, Name=@"type", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
public hrv.RespSuccess.Type type
{
get { return _type; }
set { _type = value; }
}
[global::ProtoBuf.ProtoContract(Name=@"Type")]
public enum Type
{ [global::ProtoBuf.ProtoEnum(Name=@"LOGIN", Value=)]
LOGIN = , [global::ProtoBuf.ProtoEnum(Name=@"CHANGE_PASSWORD", Value=)]
CHANGE_PASSWORD = , [global::ProtoBuf.ProtoEnum(Name=@"RESOURCE_LIST", Value=)]
RESOURCE_LIST = , [global::ProtoBuf.ProtoEnum(Name=@"SAVE_SCALE", Value=)]
SAVE_SCALE = , [global::ProtoBuf.ProtoEnum(Name=@"UPDATE_USER_INFO", Value=)]
UPDATE_USER_INFO = , [global::ProtoBuf.ProtoEnum(Name=@"GET_SCALE_LIST", Value=)]
GET_SCALE_LIST = , [global::ProtoBuf.ProtoEnum(Name=@"GET_SCALE", Value=)]
GET_SCALE =
} private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
} }

明显有一个private的extensionObject,但是不可访问。

    private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }

最终猜测使用如下方法获取

var loginResp = ProtoBuf.Extensible.GetValue<LoginResp>(response.respSuccess, );

测试代码如下

            if (ConnectServer("127.0.0.1", ))
{
var reqest = new Request() { type = Request.Type.LOGIN, };
var loginReq = new LoginReq() { username = "zhangsan", password = "" };
ProtoBuf.Extensible.AppendValue<LoginReq>(reqest, , loginReq); var stream = hrvClient.GetStream();
ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, reqest, ProtoBuf.PrefixStyle.Base128);
var response = ProtoBuf.Serializer.DeserializeWithLengthPrefix<Response>(stream, ProtoBuf.PrefixStyle.Base128); if (response.status == Response.Status.OK && response.respSuccess != null)
{
var loginResp = ProtoBuf.Extensible.GetValue<LoginResp>(response.respSuccess, ); var result = loginResp.result;
var userinfo = loginResp.userInfo;
} }

搞定!

【protobuf进阶】读取proto实体里的extensionObject对象的方法的更多相关文章

  1. 利用反射将IDataReader读取到实体类中效率低下的解决办法

    最开始使用反射一个类型的各个属性,对气进行赋值的代码如下: public static List<T> ToList<T>(IDataReader reader) { //实例 ...

  2. [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. 多次读取请求request里数据

    如果请求是GET方法,可以直接通过getParameter(String param)方法读取指定参数,可读取多次: 而POST方法的参数是存储在输入流中,只能读一次,不能多次读取. 有时需要在fil ...

  4. Flex读取txt文件里的内容(二)

    Flex读取txt文件里的内容 自己主动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8&quo ...

  5. Flex读取txt文件里的内容(一)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/you23hai45/article/details/25248307  Flex读取txt文件里的内 ...

  6. Golang的交互模式进阶-读取用户的输入

    Golang的交互模式进阶-读取用户的输入 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 读写数据除了 fmt 和 os 包,我们还需要用到 bufio 包来处理缓冲的输入和输出. ...

  7. Flex读取txt文件里的内容报错

    Flex读取txt文件里的内容 1.详细错误例如以下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativeP ...

  8. Matlab 读取文件夹里所有的文件

    (image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...

  9. EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象

    EF+LINQ事物处理   在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...

随机推荐

  1. node http.request请求

    var http = require('http'); var querystring = require('querystring'); var path = '/cricket/getRecord ...

  2. java项目测试log4j

    .literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...

  3. java设计模式——单例(Singleton)模式

    在某些场景,你需要找到一个承担职责的对象,并且这个对象是他所属类的唯一实例.此时可以使用单例模式. 单例模式的意图是为了确保一个类有且仅有一个实例,并为他提供一个全局的访问点.创建一个担当独一无二角色 ...

  4. 泛型? extents super

    ?可以接受任何泛型集合,但是不能编辑集合值.所以一般只在方法参数中用 例子: ? extends Number  则类型只能是Number类的子孙类 ? super String  则类型只能是Str ...

  5. nginx+php,502错误

    502错误基本就是php进程执行中挂了,其中有个原因就可能是进程执行超时设置导致的比如这个: ; The timeout for serving a single request after whic ...

  6. nginx——location 优先级

    一. location 的匹配符1.等于匹配符:=等于匹配符就是等号,特点可以概括为两点:精确匹配不支持正则表达式2.空匹配符空匹配符的特点是:匹配以指定模式开始的 URI不支持正则表达式3.正则匹配 ...

  7. 舵机的PWM控制学习随笔

    舵机的控制信号,对于脉宽调制信号的脉宽变换,常用的一种方法是采用调制信号获取有源滤波后的直流电压,但是需要50Hz(周期是20ms)的信号,这对运放器件的选择有较高要求,从电路体积和功耗考虑也不易采用 ...

  8. SharePoint 2013 弹窗效果之URL打开方式(一)

    在SharePoint中想做一个弹出效果其实很简单,仅仅在js中使用SharePoint Modal Dialog, 以下做一个简单的例子:很多情况下我们会通过linkButton弹出一个详细页面,那 ...

  9. linux中硬盘及网卡的表示方法

    Linux中的所有设备均表示为/dev下的一个文件,各种IDE设备分配一个由hd前缀组成的文件:而对于各种SCSI设备,则分配了一个由sd前缀组成的文件,例如: IDE0接口上的主盘成为/dev/hd ...

  10. 2. SharePoint Online 开发,请联系qq512800530。加好备注。(不要发站内信。。。)

    ///(不要发站内信...) <meta name="keywords" content="SharePoint Online, SP Online, SPO, S ...