public static SerializeTypeInfo ReadFromStream(Stream s)
        {
            UInt16 count    = s.ReadUInt16();
            var    datainfo = new SerializeTypeInfo();

            //FieldInfo
            List <SerializeFieldInfo> fieldDataList = new List <SerializeFieldInfo>();

            for (var i = 0; i < count; i++)
            {
                var fdata = SerializeFieldInfo.ReadFromStream(s);
                fieldDataList.Add(fdata);
            }
            datainfo.FieldData = fieldDataList;

            //CustomTypeInfo
            var customCount = s.ReadByte();

            if (customCount > 0)
            {
                datainfo.CustomDataTypeInfo = new List <SerializeTypeInfo>();
                for (var i = 0; i < customCount; i++)
                {
                    datainfo.CustomDataTypeInfo.Add(ReadFromStream(s));
                }
            }

            return(datainfo);
        }
        public static SerializeTypeInfo Parse(Type type, object t)
        {
            var datainfo = new SerializeTypeInfo();

            datainfo.DataType = type;
            var finfos = GetFieldInfos(type);

            datainfo.FieldData = new List <SerializeFieldInfo>();
            var customDataTypeInfo = new List <SerializeTypeInfo>();

            datainfo.CustomDataTypeInfo = customDataTypeInfo;
            for (var i = 0; i < finfos.Count; i++)
            {
                var finfo           = finfos[i];
                var fieldType       = finfo.FieldType;
                var isArray         = fieldType.IsArray;
                var elementType     = isArray ? fieldType.GetElementType() : fieldType;
                var typeEnum        = GetTypeEnum(elementType);
                var isPrimitive     = typeEnum != SerializeTypeEnum.Custom;
                var customTypeIndex = 0;
                if (isPrimitive)
                {
                }
                else
                {
                    var index = customDataTypeInfo.FindIndex(f => f.DataType == elementType);
                    if (index == -1)
                    {
                        customTypeIndex = customDataTypeInfo.Count;
                        customDataTypeInfo.Add(Parse(elementType, null));
                    }
                    else
                    {
                        customTypeIndex = index;
                    }
                }

                if (customTypeIndex > 255)
                {
                    throw new Exception("Custom type count must under 255");
                }
                var data = new SerializeFieldInfo
                {
                    FieldActive     = true,
                    HasLabel        = false,
                    FieldType       = fieldType,
                    ElementType     = elementType,
                    IsArray         = isArray,
                    IsPrimitive     = isPrimitive,
                    FieldName       = finfo.Name,
                    TypeEnum        = typeEnum,
                    CustomTypeIndex = (byte)customTypeIndex,
                };

                datainfo.FieldData.Add(data);
            }
            return(datainfo);
        }