Пример #1
0
        private static ObjectActivator ListActivator(Type expectedType)
        {
            var genericArgs = expectedType.GetGenericArguments();
            var argType     = genericArgs.Length switch
            {
                0 => typeof(object),
                1 => genericArgs[0],
                _ => throw new DeserializationException(
                          $"Unexpected number of generic arguments for list: {genericArgs.Length}"),
            };
            ConstructorInfo constructor;
            var             interfaceType = typeof(ICollection <>).MakeGenericType(argType);
            var             listType      = typeof(List <>).MakeGenericType(argType);

            if (expectedType.IsAssignableFrom(listType))
            {
                constructor = listType.GetConstructor(new[] { typeof(int) });
            }
            else
            {
                ReflectionUtil.CheckType(interfaceType, expectedType);
                constructor = expectedType.GetConstructor(Type.EmptyTypes);
                if (constructor == null)
                {
                    throw new DeserializationException($"Unable to find default constructor for {expectedType}");
                }
            }
            var activator = ReflectionUtil.CreateActivator(constructor);

            return(activator);
        }
        private static ObjectActivator DictionaryActivator(Type expectedType)
        {
            var genericArgs = expectedType.GetGenericArguments();

            if (genericArgs.Length != 2)
            {
                throw new DeserializationException(
                          $"Unexpected number of Dictionary generic arguments: {genericArgs.Length}");
            }
            ConstructorInfo constructor;

            if (expectedType.IsInterface)
            {
                var dictType = typeof(Dictionary <,>).MakeGenericType(genericArgs);
                ReflectionUtil.CheckType(expectedType, dictType);
                constructor = dictType.GetConstructor(new[] { typeof(int) });
            }
            else
            {
                ReflectionUtil.CheckType(typeof(IDictionary), expectedType);
                constructor = expectedType.GetConstructor(Type.EmptyTypes);
                if (constructor == null)
                {
                    throw new DeserializationException($"Unable to find default constructor for {expectedType}");
                }
            }
            var activator = ReflectionUtil.CreateActivator(constructor);

            return(activator);
        }
        /// <summary>
        ///     Decodes the boolean.
        /// </summary>
        /// <param name="expectedType"></param>
        /// <param name="size">The size of the structure.</param>
        /// <returns></returns>
        private static bool DecodeBoolean(Type expectedType, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(bool));

            return(size switch
            {
                0 => false,
                1 => true,
                _ => throw new InvalidDatabaseException("The MaxMind DB file's data section contains bad data: "
                                                        + "invalid size of boolean."),
            });
Пример #4
0
        /// <summary>
        ///     Decodes the float.
        /// </summary>
        /// <returns></returns>
        private float DecodeFloat(Type expectedType, long offset, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(float));

            if (size != 4)
            {
                throw new InvalidDatabaseException("The MaxMind DB file's data section contains bad data: "
                                                   + "invalid size of float.");
            }
            return(_database.ReadFloat(offset));
        }
Пример #5
0
        /// <summary>
        ///     Decodes the double.
        /// </summary>
        /// <returns></returns>
        private double DecodeDouble(Type expectedType, long offset, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(double));

            if (size != 8)
            {
                throw new InvalidDatabaseException("The MaxMind DB file's data section contains bad data: "
                                                   + "invalid size of double.");
            }
            return(_database.ReadDouble(offset));
        }
Пример #6
0
        /// <summary>
        ///     Decodes the boolean.
        /// </summary>
        /// <param name="expectedType"></param>
        /// <param name="size">The size of the structure.</param>
        /// <returns></returns>
        private static bool DecodeBoolean(Type expectedType, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(bool));

            switch (size)
            {
            case 0:
                return(false);

            case 1:
                return(true);

            default:
                throw new InvalidDatabaseException("The MaxMind DB file's data section contains bad data: "
                                                   + "invalid size of boolean.");
            }
        }
Пример #7
0
        /// <summary>
        ///     Decodes the integer.
        /// </summary>
        /// <returns></returns>
        private int DecodeInteger(Type expectedType, long offset, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(int));

            return(_database.ReadInteger(0, offset, size));
        }
Пример #8
0
 /// <summary>
 ///     Decodes the big integer.
 /// </summary>
 /// <returns></returns>
 private BigInteger DecodeBigInteger(Type expectedType, long offset, int size)
 {
     ReflectionUtil.CheckType(expectedType, typeof(BigInteger));
     return(_database.ReadBigInteger(offset, size));
 }
Пример #9
0
 /// <summary>
 ///     Decodes the uint64.
 /// </summary>
 /// <returns></returns>
 private ulong DecodeUInt64(Type expectedType, long offset, int size)
 {
     ReflectionUtil.CheckType(expectedType, typeof(ulong));
     return(_database.ReadULong(offset, size));
 }
Пример #10
0
        private byte[] DecodeBytes(Type expectedType, long offset, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(byte[]));

            return(_database.Read(offset, size));
        }
Пример #11
0
        /// <summary>
        ///     Decodes the string.
        /// </summary>
        /// <returns></returns>
        private string DecodeString(Type expectedType, long offset, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(string));

            return(_database.ReadString(offset, size));
        }