Exemplo n.º 1
0
            /// <summary>
            /// reads a type from the binary reader
            /// </summary>
            /// <param name="reader">the binary reader</param>
            /// <returns>a type based off the type code that is read</returns>
            private static Type getType(BinaryReader reader)
            {
                switch (BinaryReaderExtension.ReadType <TypeCode>(reader))
                {
                case TypeCode.Null:
                    return(null);

                    break;

                case TypeCode.Array:
                    return(Array.CreateInstance(getType(reader), 0).GetType());

                    break;

                case TypeCode.Bool:
                    return(typeof(bool));

                    break;

                case TypeCode.Byte:
                    return(typeof(byte));

                    break;

                case TypeCode.SByte:
                    return(typeof(sbyte));

                    break;

                case TypeCode.Char:
                    return(typeof(char));

                    break;

                case TypeCode.Int:
                    return(typeof(int));

                    break;

                case TypeCode.UInt:
                    return(typeof(uint));

                    break;

                case TypeCode.UShort:
                    return(typeof(ushort));

                    break;

                case TypeCode.ULong:
                    return(typeof(ulong));

                    break;

                case TypeCode.Short:
                    return(typeof(short));

                    break;

                case TypeCode.Long:
                    return(typeof(long));

                    break;

                case TypeCode.Decimal:
                    return(typeof(decimal));

                    break;

                case TypeCode.Float:
                    return(typeof(float));

                    break;

                case TypeCode.Double:
                    return(typeof(double));

                    break;

                case TypeCode.Object:
                    return(typeof(object));

                    break;

                case TypeCode.String:
                    return(typeof(string));

                    break;

                default:
                    throw new Exception("invalid type");
                    break;
                }
            }
Exemplo n.º 2
0
            private static dynamic getValue(Type type, BinaryReader reader)
            {
                if (type == null)
                {
                    return(null);
                }
                else
                {
                    if (type.IsArray)
                    {
                        //do array stuff
                        int length = reader.ReadInt32();

                        if (length < 0)
                        {
                            return(null);
                        }
                        else
                        {
                            dynamic[] output = new dynamic[length];

                            for (int i = 0; i < length; i++)
                            {
                                output[i] = getValue(type.GetElementType(), reader);
                            }

                            return(output);
                        }
                    }
                    else
                    {
                        var obj = BinaryReaderExtension.ReadType(type, reader);
                        if (obj != null)
                        {
                            return(obj);
                        }
                        else
                        {
                            //do object stuff
                            var output     = new ExpandoObject() as IDictionary <string, object>;
                            int properties = reader.ReadInt32();

                            if (properties < 0) //checks for nulls
                            {
                                return(null);
                            }
                            else
                            {
                                for (int i = 0; i < properties; i++)
                                {
                                    string name = reader.ReadString();

                                    output.Add(name, Deserialize(reader));
                                }

                                return(output);
                            }
                        }
                    }
                }
            }