//设置扩展对象
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. hadoop 分片与分块,map task和reduce task的理解

    分块:Block HDFS存储系统中,引入了文件系统的分块概念(block),块是存储的最小单位,HDFS定义其大小为64MB.与单磁盘文件系统相似,存储在 HDFS上的文件均存储为多个块,不同的是, ...

  2. 关于使用iframe标签自适应高度的使用

    在ifrome内设定最小高度,(此方法只适用于页面内切换高度不一.但是会保留最大高度,返回后保持最大高度不再回到最初页面的高度) <iframe id="one4" widt ...

  3. PAT - 基础 - 龟兔赛跑

    题目: 乌龟与兔子进行赛跑,跑场是一个矩型跑道,跑道边可以随地进行休息.乌龟每分钟可以前进3米,兔子每分钟前进9米:兔子嫌乌龟跑得慢,觉得肯定能跑赢乌龟,于是,每跑10分钟回头看一下乌龟,若发现自己超 ...

  4. ThinkPHP框架下,jq实现在div中添加标签并且div的大小会随之变化

    php初学者,有什么不对的还请指正. 首先是在html页面中用jq实现添加标签:divAchivePersonnal是select所在的div的外层div,divselectAchivePersonn ...

  5. MySql数据库3【优化1】表的优化

    一.表结构的优化 1.标准化  标准化是在数据库中组织数据的过程.其中包括,根据设计规则创建表并在这些表间建立关系:通过取消冗余度与不一致相关性,该设计规则可以同时保护数据并提高数据的灵活性.通常数据 ...

  6. 七天学会 SALT STACK 自动化运维 (1)

    七天学会 SALT STACK 自动化运维 (1) 简单理解 SALTSTACK 安装与配置 基本的使用方法 结束语 引用资源 简单理解 SALT STACK 笔者是初次接触 自动化运维 这一技术领域 ...

  7. asp.net mvc 强类型视图中传入List 数据到控制器

    问题来源: 在和一位技术老师聊天时,老师问我一个mvc 表单提交的问题,问道:怎样在表单提交的时候,将 带有 List 属性的对象传入控制器? 这时,我有点呆了,以前一直都好像是 单一属性的表单提交, ...

  8. MATLAB中多行注释的三种方法

    MATLAB中多行注释的三种方法 A. %{ 若干语句 %} B. 多行注释: 选中要注释的若干语句, 编辑器菜单Text->Comment, 或者快捷键Ctrl+R 取消注释: 选中要取消注释 ...

  9. A Knight's Journey

    poj2488:http://poj.org/problem?id=2488 题意:给你一张地图,然后有一个骑士,骑士可以从地图的任意一个方格开始,作为起点,问你该骑士能否走遍整张题图.题解:首先想到 ...

  10. PSoC电容式触摸感应技术

    PSoC是由Cypress半导体公司推出的具有数字和模拟混合处理能力的可编程片上系统芯片,某些系列的PSoC(如CY8C21X34系列),由于其内部配备的特殊资源,使得它可以很容易地实现电容式触摸感应 ...