/// <summary> /// 反序列化对象,利用CMessage填充对象 /// </summary> /// <param name="msg">CMessage对象</param> public void FromMessage(ref CMessage msg) { Type insType = this.GetType(); // 获得本实例下所有的公开属性 FieldInfo[] fis = insType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); for (int i = 0; i < fis.Length; i++) { Type MemberType = fis[i].FieldType; if (CheckCanSerialize(MemberType)) { object[] param = { msg }; object PopValue = CFunction.CallGenericFunction("PopData", msg.Body, MemberType, param); // 设置值 fis[i].SetValue(this, PopValue); } } }
/// <summary> /// 序列化对象,获得值填充CMessage /// </summary> /// <returns> 待填充的CMessage对象 </returns> public void ToMessage(ref CMessage msg) { // 获得当前实例类型 Type insType = this.GetType(); // 获得本实例下所有的公开属性 FieldInfo[] fis = insType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); // 遍历属性 for (int i = 0; i < fis.Length; i++) { // 属性类型 Type MemberType = fis[i].FieldType; if (CheckCanSerialize(MemberType)) { object[] param = { fis[i].GetValue(this), msg }; CFunction.CallGenericFunction("PushData", msg.Body, MemberType, param); } } }
/// <summary> /// 从body中获取数据,写入参数中去 /// </summary> /// <typeparam name="T">泛型</typeparam> public T PopData <T>(ref CMessage msg) { Type TType = typeof(T); if (TType == typeof(byte)) { return(( T )( Object )PopByte()); } if (TType == typeof(sbyte)) { return(( T )( Object )PopSByte()); } if (TType == typeof(bool)) { return(( T )( Object )PopBoolean()); } if (TType == typeof(short)) { return(( T )( Object )PopShort()); } if (TType == typeof(ushort)) { return(( T )( Object )PopUShort()); } if (TType == typeof(int)) { return(( T )( Object )PopInt()); } if (TType == typeof(uint)) { return(( T )( Object )PopUInt()); } if (TType == typeof(float)) { return(( T )( Object )PopFloat()); } if (TType == typeof(long)) { return(( T )( Object )PopLong()); } if (TType == typeof(ulong)) { return(( T )( Object )PopULong()); } if (TType == typeof(string)) { return(( T )( Object )PopString()); } // 如果反射对象是 CPackageBase 的子类 if (TType.IsSubclassOf(typeof(CPackageBase))) { // 创建反射类型实例 CPackageBase MemberPackageBase = Activator.CreateInstance(TType) as CPackageBase; MemberPackageBase.FromMessage(ref msg); return(( T )( Object )MemberPackageBase); } // 如果反射对象是 List if (TType.IsGenericType) { Type[] GenericTypes = TType.GetGenericArguments(); object[] param = { msg }; return(( T )( Object )CFunction.CallGenericArrayFunction("PopList", this, GenericTypes, param)); } // 如果反射对象是数组 if (TType.IsArray) { Type ElementType = TType.GetElementType(); object[] param = { msg }; return(( T )( Object )CFunction.CallGenericFunction("PopArray", this, ElementType, param)); } CLOG.E("the type {0} has no serilize methon", TType.ToString()); return(default(T)); }
/// <summary> /// 压入数据 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="data">要压入的值></param> /// <param name="msg">要压入的目标消息></param> public void PushData <T>(T data, ref CMessage msg) { Type dataType = typeof(T); if (dataType == typeof(byte)) { PushByte(( byte )( Object )data); return; } if (dataType == typeof(sbyte)) { PushByte(( sbyte )( Object )data); return; } if (dataType == typeof(bool)) { PushBoolean(( bool )( Object )data); return; } if (dataType == typeof(int)) { PushInt(( int )( Object )data); return; } if (dataType == typeof(uint)) { PushInt(( uint )( Object )data); return; } if (dataType == typeof(short)) { PushShort(( short )( Object )data); return; } if (dataType == typeof(ushort)) { PushInt(( ushort )( Object )data); return; } if (dataType == typeof(float)) { PushFloat(( float )( Object )data); return; } if (dataType == typeof(long)) { PushLong(( long )( Object )data); return; } if (dataType == typeof(ulong)) { PushLong(( ulong )( Object )data); return; } if (dataType == typeof(string)) { PushString(( string )( Object )data); return; } // 如果反射对象是 CPackageBase 的子类 if (data is CPackageBase) { CPackageBase tempData = ( CPackageBase )( Object )data; if (tempData == null) { return; } tempData.ToMessage(ref msg); return; } // 如果反射对象是 List if (dataType.IsGenericType) { Type[] GenericType = dataType.GetGenericArguments(); object[] param = { data, msg }; CFunction.CallGenericArrayFunction("PushList", this, GenericType, param); return; } // 如果反射对象是数组 if (dataType.IsArray) { Type ElementType = dataType.GetElementType(); object[] param = { data, msg }; CFunction.CallGenericFunction("PushArray", this, ElementType, param); return; } CLOG.E("the type {0} has no serilize methon", dataType.ToString()); }