示例#1
0
 /// <summary>
 /// 将对象转换为二进制
 /// </summary>
 /// <param name="o"></param>
 /// <param name="pkg"></param>
 /// <returns></returns>
 public static IMsgOutPutByteArray ObjectToPackage(object o, IMsgOutPutByteArray pkg)
 {
     if (o is Array)
     {
         _ArrayToPackage(o as Array, pkg, o.GetType());
     }
     else
     {
         _ObjectToPackage(o, pkg);
     }
     return(pkg);
 }
示例#2
0
        /// <summary>
        /// 将数组转换成二进制
        /// </summary>
        /// <param name="array"></param>
        /// <param name="pkg"></param>
        /// <param name="type"></param>
        private static void _ArrayToPackage(Array array, IMsgOutPutByteArray pkg, Type type)
        {
            short count = (short)array.Length;

            pkg.PushShort(count);
            int i;

            if (type == typeof(bool))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushByte((bool)array.GetValue(i) == true ? (byte)1 : (byte)0);
                }
            }
            else if (type == typeof(sbyte))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushSByte((sbyte)array.GetValue(i));
                }
            }
            else if (type == typeof(byte))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushByte((byte)array.GetValue(i));
                }
            }
            else if (type == typeof(short))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushShort((short)array.GetValue(i));
                }
            }
            else if (type == typeof(ushort))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushUShort((ushort)array.GetValue(i));
                }
            }
            else if (type == typeof(int))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushInt((int)array.GetValue(i));
                }
            }
            else if (type == typeof(uint))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushUInt((uint)array.GetValue(i));
                }
            }
            else if (type == typeof(long))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushLong((long)array.GetValue(i));
                }
            }
            else if (type == typeof(ulong))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushULong((ulong)array.GetValue(i));
                }
            }
            else if (type == typeof(string))
            {
                for (i = 0; i < count; i++)
                {
                    pkg.PushString((string)array.GetValue(i));
                }
            }
            else
            {
                for (i = 0; i < count; i++)
                {
                    _ObjectToPackage(array.GetValue(i), pkg);
                }
            }
        }
示例#3
0
 /// <summary>
 /// 将对象转换为二进制
 /// </summary>
 /// <param name="o"></param>
 /// <param name="pkg"></param>
 private static void _ObjectToPackage(object o, IMsgOutPutByteArray pkg)
 {
     try
     {
         FieldInfo[] fields = o.GetType().GetFields();
         foreach (FieldInfo field in fields)
         {
             if (field.FieldType == typeof(bool))
             {
                 pkg.PushByte((bool)field.GetValue(o) == true ? (byte)1 : (byte)0);
             }
             else if (field.FieldType == typeof(sbyte))
             {
                 pkg.PushSByte((sbyte)field.GetValue(o));
             }
             else if (field.FieldType == typeof(byte))
             {
                 pkg.PushByte((byte)field.GetValue(o));
             }
             else if (field.FieldType == typeof(short))
             {
                 pkg.PushShort((short)field.GetValue(o));
             }
             else if (field.FieldType == typeof(ushort))
             {
                 pkg.PushUShort((ushort)field.GetValue(o));
             }
             else if (field.FieldType == typeof(int))
             {
                 pkg.PushInt((int)field.GetValue(o));
             }
             else if (field.FieldType == typeof(uint))
             {
                 pkg.PushUInt((uint)field.GetValue(o));
             }
             else if (field.FieldType == typeof(long))
             {
                 pkg.PushLong((long)field.GetValue(o));
             }
             else if (field.FieldType == typeof(ulong))
             {
                 pkg.PushULong((ulong)field.GetValue(o));
             }
             else if (field.FieldType == typeof(string))
             {
                 pkg.PushString((string)field.GetValue(o));
             }
             else
             {
                 Type subType = field.FieldType.GetElementType();
                 if (subType == null)
                 {
                     _ObjectToPackage(field.GetValue(o), pkg);
                 }
                 else
                 {
                     _ArrayToPackage(field.GetValue(o) as Array, pkg, subType);
                 }
             }
         }
     }
     catch (Exception e)
     {
         throw new UnityEngine.UnityException("Error: ObjectToPackage error -- " + e.ToString());
     }
 }