//设置扩展对象
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. Jquery 点击空白处消失

    $(document).bind("click", function (e){ if ( $((e.target || e.srcElement)).closest("# ...

  2. 64位系统下System32文件系统重定向

    前言 因为一次偶然的机会,需要访问系统目录“C:/Windows/System32“文件夹下的内容,使用的测试机器上预装了win7 64系统.在程序运行中竟然发生了该文件路径不存在的问题!!通过查看网 ...

  3. SGU 158.Commuter Train

    一道简单题. 火车停的位置不是在整点就是在二分之一点,坐标*2,然后枚举火车停的位置,计算总距离即可. code: #include <iostream> #include <cma ...

  4. linux如何开机以命令行形式启动?

    在管理员权限下,修改/etc/inittab文件即可.把id:5:initdefault:改为id:3:initdefault:就可以了. 如下图所示: 图1: . 图2:

  5. centos6.2下搭建Web服务器

    1.安装Apache2 yum install httpd 2.启动 方法一:service httpd start 方法二:/etc/init.d/httpd start //浏览http://ip ...

  6. Bootstrap_Javascript_图片轮播

    一 . 结构分析 一个轮播图片主要包括三个部分: ☑ 轮播的图片 ☑ 轮播图片的计数器 ☑ 轮播图片的控制器 第一步:设计轮播图片的容器.在 Bootstrap 框架中采用 carousel 样式,并 ...

  7. WordPress批量修改文章内容、URL链接、文章摘要

    通过SQL语句来批量修改wordpress博客内容,文章中所有语句都使用默认的wp_表前缀,如果您的数据表前缀不是wp_则需要在语句中作相应更改. 方法/步骤   批量修改文章内容 如果您想替换之前写 ...

  8. Help Me with the Game

    Help Me with the GameCrawling in process... Crawling failed Description Your task is to read a pictu ...

  9. C语言宏定义使用技巧

    写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性 等等.下面列举一些成熟软件中常用得宏定义...... 1.防止一个头文件被重复包含 #ifndef COMDEF_ ...

  10. c# 类;一维数组;二维数组

    1. 输入邮箱帐号,判断格式是否正确  (1)有且只有一个@          Contains IndexOf ==LastIndexOf  (2)不能以@开头           StartsWi ...