Пример #1
0
 private object ReadObject(object instance, BinaryMemberAttribute attribute, Type type)
 {
     if (attribute.Converter == null)
     {
         if (type == typeof(String))
         {
             if (attribute.StringFormat == StringDataFormat.Raw)
             {
                 return(ReadString(attribute.Length));
             }
             else
             {
                 return(ReadString(attribute.StringFormat));
             }
         }
         else if (type.IsEnumerable())
         {
             throw new InvalidOperationException("Multidimensional arrays cannot be read directly.");
         }
         else if (type == typeof(Boolean))
         {
             return(ReadBoolean(attribute.BooleanFormat));
         }
         else if (type == typeof(Byte))
         {
             return(ReadByte());
         }
         else if (type == typeof(DateTime))
         {
             return(ReadDateTime(attribute.DateTimeFormat));
         }
         else if (type == typeof(Decimal))
         {
             return(ReadDecimal());
         }
         else if (type == typeof(Double))
         {
             return(ReadDouble());
         }
         else if (type == typeof(Int16))
         {
             return(ReadInt16());
         }
         else if (type == typeof(Int32))
         {
             return(ReadInt32());
         }
         else if (type == typeof(Int64))
         {
             return(ReadInt64());
         }
         else if (type == typeof(SByte))
         {
             return(ReadSByte());
         }
         else if (type == typeof(Single))
         {
             return(ReadSingle());
         }
         else if (type == typeof(UInt16))
         {
             return(ReadUInt16());
         }
         else if (type == typeof(UInt32))
         {
             return(ReadUInt32());
         }
         else if (type == typeof(UInt64))
         {
             return(ReadUInt64());
         }
         else if (type.GetTypeInfo().IsEnum)
         {
             return(ReadEnum(type, attribute.Strict));
         }
         else
         {
             return(ReadCustomObject(type, null, Position));
         }
     }
     else
     {
         // Let a converter do all the work.
         IBinaryConverter converter = BinaryConverterCache.GetConverter(attribute.Converter);
         return(converter.Read(this, instance, attribute));
     }
 }
Пример #2
0
 private static object ReadObject(Stream stream, object instance, BinaryMemberAttribute attribute,
                                  Type type, ByteConverter converter)
 {
     if (attribute.Converter == null)
     {
         if (type == typeof(String))
         {
             if (attribute.StringFormat == StringCoding.Raw)
             {
                 return(stream.ReadString(attribute.Length));
             }
             else
             {
                 return(stream.ReadString(attribute.StringFormat, converter: converter));
             }
         }
         else if (type.IsEnumerable())
         {
             throw new InvalidOperationException("Multidimensional arrays cannot be read directly.");
         }
         else if (type == typeof(Boolean))
         {
             return(stream.ReadBoolean(attribute.BooleanFormat));
         }
         else if (type == typeof(Byte))
         {
             return(stream.Read1Byte());
         }
         else if (type == typeof(DateTime))
         {
             return(stream.ReadDateTime(attribute.DateTimeFormat, converter));
         }
         else if (type == typeof(Decimal))
         {
             return(stream.ReadDecimal());
         }
         else if (type == typeof(Double))
         {
             return(stream.ReadDouble(converter));
         }
         else if (type == typeof(Int16))
         {
             return(stream.ReadInt16(converter));
         }
         else if (type == typeof(Int32))
         {
             return(stream.ReadInt32(converter));
         }
         else if (type == typeof(Int64))
         {
             return(stream.ReadInt64(converter));
         }
         else if (type == typeof(SByte))
         {
             return(stream.ReadSByte());
         }
         else if (type == typeof(Single))
         {
             return(stream.ReadSingle(converter));
         }
         else if (type == typeof(UInt16))
         {
             return(stream.ReadUInt16(converter));
         }
         else if (type == typeof(UInt32))
         {
             return(stream.ReadUInt32(converter));
         }
         else if (type == typeof(UInt64))
         {
             return(stream.ReadUInt64(converter));
         }
         else if (type.IsEnum)
         {
             return(ReadEnum(stream, type, attribute.Strict, converter));
         }
         else
         {
             if (stream.CanSeek)
             {
                 return(ReadCustomObject(stream, type, null, stream.Position, converter));
             }
             else
             {
                 return(ReadCustomObject(stream, type, null, -1, converter));
             }
         }
     }
     else
     {
         // Let a binary converter do all the work.
         IBinaryConverter binaryConverter = BinaryConverterCache.GetConverter(attribute.Converter);
         return(binaryConverter.Read(stream, instance, attribute, converter));
     }
 }