Exemplo n.º 1
0
        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.GetTypeInfo().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);
        }
Exemplo n.º 2
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 AiWen DB file's data section contains bad data: "
                                                   + "invalid size of float.");
            }
            return(_database.ReadFloat(offset));
        }
Exemplo n.º 3
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 AiWen DB file's data section contains bad data: "
                                                   + "invalid size of double.");
            }
            return(_database.ReadDouble(offset));
        }
Exemplo n.º 4
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 AiWen DB file's data section contains bad data: "
                                                   + "invalid size of boolean.");
            }
        }
Exemplo n.º 5
0
        private static ObjectActivator ListActivator(Type expectedType)
        {
            var  genericArgs = expectedType.GetGenericArguments();
            Type argType;

            switch (genericArgs.Length)
            {
            case 0:
                argType = typeof(object);
                break;

            case 1:
                argType = genericArgs[0];
                break;

            default:
                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);
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
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));
 }
Exemplo n.º 8
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));
 }
Exemplo n.º 9
0
        private byte[] DecodeBytes(Type expectedType, long offset, int size)
        {
            ReflectionUtil.CheckType(expectedType, typeof(byte[]));

            return(_database.Read(offset, size));
        }
Exemplo n.º 10
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));
        }