Exemplo n.º 1
0
        public static object DeserializeObject(BinaryReader reader, int code)
        {
            object   obj;
            TypeInfo t;

            try
            {
                t   = TlContext.GetType((int)code).GetTypeInfo();
                obj = Activator.CreateInstance(t.AsType());
            }
            catch (Exception ex)
            {
                throw new InvalidDataException("Constructor Invalid Or Context.Init Not Called !", ex);
            }
            if (t.IsSubclassOf(typeof(TlMethod)))
            {
                ((TlMethod)obj).DeserializeResponse(reader);
                return(obj);
            }
            if (t.IsSubclassOf(typeof(TlObject)))
            {
                ((TlObject)obj).DeserializeBody(reader);
                return(obj);
            }
            throw new NotImplementedException("Weird Type : " + t.Namespace + " | " + t.Name);
        }
Exemplo n.º 2
0
        public override void DeserializeBody(BinaryReader br)
        {
            var count = br.ReadInt32();

            for (var i = 0; i < count; i++)
            {
                if (typeof(T) == typeof(int))
                {
                    Lists.Add((T)Convert.ChangeType(br.ReadInt32(), typeof(T)));
                }
                else if (typeof(T) == typeof(long))
                {
                    Lists.Add((T)Convert.ChangeType(br.ReadInt64(), typeof(T)));
                }
                else if (typeof(T) == typeof(string))
                {
                    Lists.Add((T)Convert.ChangeType(StringUtil.Deserialize(br), typeof(T)));
                }
                else if (typeof(T) == typeof(double))
                {
                    Lists.Add((T)Convert.ChangeType(br.ReadDouble(), typeof(T)));
                }
                else if (typeof(T).GetTypeInfo().BaseType == typeof(TlObject))
                {
                    var constructor = br.ReadInt32();
                    var type        = TlContext.GetType(constructor);
                    var obj         = Activator.CreateInstance(type);
                    type.GetTypeInfo().GetMethod("DeserializeBody").Invoke(obj, new object[] { br });
                    Lists.Add((T)Convert.ChangeType(obj, type));
                }
            }
        }