Exemplo n.º 1
0
        /// <summary>
        /// 尝试直接反序列化基础值类型(包括其数组)
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="value">返回的对象</param>
        /// <param name="theByteReader">读取reader</param>
        /// <returns>能否执行</returns>
        public static bool TryGetValue(Type type, out object value, ZipBytesReader theByteReader)
        {
            value = null;

            if (type.IsEnum)
            {
                type = type.GetFields()[0].FieldType;
            }

            if (type == Type_Bool)
            {
                value = theByteReader.ReadBoolean();
                return(true);
            }
            if (type == Type_Byte)
            {
                value = theByteReader.ReadByte();
                return(true);
            }
            if (type == Type_SByte)
            {
                value = theByteReader.ReadSByte();
                return(true);
            }
            if (type == Type_UInt16)
            {
                value = theByteReader.ReadUInt16();
                return(true);
            }
            if (type == Type_Int16)
            {
                value = theByteReader.ReadInt16();
                return(true);
            }
            if (type == Type_Char)
            {
                value = theByteReader.ReadChar();
                return(true);
            }
            if (type == Type_UInt32)
            {
                value = theByteReader.ReadUInt32();
                return(true);
            }
            if (type == Type_Int32)
            {
                value = theByteReader.ReadInt32();
                return(true);
            }
            if (type == Type_UInt64)
            {
                value = theByteReader.ReadUInt64();
                return(true);
            }
            if (type == Type_Int64)
            {
                value = theByteReader.ReadInt64();
                return(true);
            }
            if (type == Type_Single)
            {
                value = theByteReader.ReadSingle();
                return(true);
            }
            if (type == Type_Double)
            {
                value = theByteReader.ReadDouble();
                return(true);
            }
            if (type == Type_Decimal)
            {
                value = theByteReader.ReadDecimal();
                return(true);
            }
            if (type == Type_Guid)
            {
                value = theByteReader.ReadGuid();
                return(true);
            }
            if (type == Type_DateTime)
            {
                value = theByteReader.ReadDateTime();
                return(true);
            }
            if (type == Type_TimeSpan)
            {
                value = theByteReader.ReadTimeSpan();
                return(true);
            }
            if (type == Type_String)
            {
                value = theByteReader.ReadString();
                return(true);
            }

            bool      isEnumArray     = false;
            Type      elemtT          = null;
            FieldInfo elemtValueField = null;

            if (type.IsArray)
            {
                elemtT = type.GetElementType();
                if (elemtT.IsEnum)
                {
                    isEnumArray = true;

                    elemtValueField = elemtT.GetFields()[0];

                    type = elemtValueField.FieldType.MakeArrayType();
                }
            }

            if (type == Type_Bools)
            {
                value = theByteReader.ReadBooleans();
                goto GoTrueEnd;
            }
            if (type == Type_Bytes)
            {
                value = theByteReader.ReadBytes();
                goto GoTrueEnd;
            }
            if (type == Type_SBytes)
            {
                value = theByteReader.ReadSBytes();
                goto GoTrueEnd;
            }
            if (type == Type_UInt16s)
            {
                value = theByteReader.ReadUInt16s();
                goto GoTrueEnd;
            }
            if (type == Type_Int16s)
            {
                value = theByteReader.ReadInt16s();
                goto GoTrueEnd;
            }
            if (type == Type_Chars)
            {
                value = theByteReader.ReadChars();
                goto GoTrueEnd;
            }
            if (type == Type_UInt32s)
            {
                value = theByteReader.ReadUInt32s();
                goto GoTrueEnd;
            }
            if (type == Type_Int32s)
            {
                value = theByteReader.ReadInt32s();
                goto GoTrueEnd;
            }
            if (type == Type_UInt64s)
            {
                value = theByteReader.ReadUInt64s();
                goto GoTrueEnd;
            }
            if (type == Type_Int64s)
            {
                value = theByteReader.ReadInt64s();
                goto GoTrueEnd;
            }
            if (type == Type_Singles)
            {
                value = theByteReader.ReadSingles();
                goto GoTrueEnd;
            }
            if (type == Type_Doubles)
            {
                value = theByteReader.ReadDoubles();
                goto GoTrueEnd;
            }
            if (type == Type_Decimals)
            {
                value = theByteReader.ReadDecimals();
                goto GoTrueEnd;
            }
            if (type == Type_Guids)
            {
                value = theByteReader.ReadGuids();
                goto GoTrueEnd;
            }
            if (type == Type_DateTimes)
            {
                value = theByteReader.ReadDateTimes();
                goto GoTrueEnd;
            }
            if (type == Type_TimeSpans)
            {
                value = theByteReader.ReadTimeSpans();
                goto GoTrueEnd;
            }

            return(false);

GoTrueEnd:
            if (isEnumArray && value != null)
            {
                Array oArray      = value as Array;
                int   arrayLength = oArray.Length;

                Array newArray = Array.CreateInstance(elemtT, arrayLength);

                for (int i = 0; i < arrayLength; i++)
                {
                    newArray.SetValue(Enum.ToObject(elemtT, oArray.GetValue(i)), i);
                }

                value = newArray;
            }
            return(true);
        }