【protobuf进阶】读取proto实体里的extensionObject对象的方法
//设置扩展对象
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对象的方法的更多相关文章
- 利用反射将IDataReader读取到实体类中效率低下的解决办法
最开始使用反射一个类型的各个属性,对气进行赋值的代码如下: public static List<T> ToList<T>(IDataReader reader) { //实例 ...
- [原创]java WEB学习笔记59:Struts2学习之路---OGNL,值栈,读取对象栈中的对象的属性,读取 Context Map 里的对象的属性,调用字段和方法,数组,list,map
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 多次读取请求request里数据
如果请求是GET方法,可以直接通过getParameter(String param)方法读取指定参数,可读取多次: 而POST方法的参数是存储在输入流中,只能读一次,不能多次读取. 有时需要在fil ...
- Flex读取txt文件里的内容(二)
Flex读取txt文件里的内容 自己主动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8&quo ...
- Flex读取txt文件里的内容(一)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/you23hai45/article/details/25248307 Flex读取txt文件里的内 ...
- Golang的交互模式进阶-读取用户的输入
Golang的交互模式进阶-读取用户的输入 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 读写数据除了 fmt 和 os 包,我们还需要用到 bufio 包来处理缓冲的输入和输出. ...
- Flex读取txt文件里的内容报错
Flex读取txt文件里的内容 1.详细错误例如以下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativeP ...
- Matlab 读取文件夹里所有的文件
(image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...
- EF+LINQ事物处理 C# 使用NLog记录日志入门操作 ASP.NET MVC多语言 仿微软网站效果(转) 详解C#特性和反射(一) c# API接受图片文件以Base64格式上传图片 .NET读取json数据并绑定到对象
EF+LINQ事物处理 在使用EF的情况下,怎么进行事务的处理,来减少数据操作时的失误,比如重复插入数据等等这些问题,这都是经常会遇到的一些问题 但是如果是我有多个站点,然后存在同类型的角色去操作 ...
随机推荐
- java.lang.reflection打印一个类的全部信息
package com.ljy.chapter5; import java.lang.reflect.Constructor; import java.lang.reflect.Field; impo ...
- Codeforces 474F - Ant colony
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...
- PHP-HTML重要知识点笔记
1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...
- ASP.NET MVC 过滤器Filter
在Asp.netMvc中当你有以下及类似以下需求时你可以使用Filter功能 判断登录与否或用户权限 决策输出缓存 防盗链 防蜘蛛 本地化与国际化设置 实现动态Action Filter是一种声明式编 ...
- SSI指令教程
一:概述 SSI:服务器端嵌入或者叫服务器端包含,是Server Side Include的简写.SSI技术通过在文档中加入SSI指令,让服务器端在输出文档之前解析SSI指令,并把解析完的结果和文档一 ...
- 关于 self 和 super 在oc 中 的疑惑 与 分析
关于 self 和 super 在oc 中 的疑惑 与 分析 面试一定都是很注重 基础的,不管高级还是初级. 虽然基础好跟基础不好都可以写 代码,网上那么多资料. 区分高低也就是研究的深度和广度 ...
- 转:100个高质量Java开发者博客
原文来自于:http://www.importnew.com/7469.html ImportNew注:原文中还没有100个.作者希望大家一起来推荐高质量的Java开发博客,然后不段补充到这个列表.欢 ...
- 转:2014 年 15 款新评定的最佳 PHP 框架
原文来自于:http://blog.jobbole.com/59999/ 原文出处: codegeekz 译文出处:oschina 欢迎分享原创到伯乐头条 通常,框架都会被认为是帮助开发者快速 ...
- 接受、online、见刊时,期刊的 IF 都不同,究竟算发几分期刊?
- 关于javascript
Client-Language-----------------------JavaScript/Object-C/Java/C# HTML5 DOM/Template/Data/Ajax/Regul ...