예제 #1
0
        public static object Deserialize(List <byte> byteList, Type type)
        {
            UInt32 expectedClassId = type.GetClassId();

            if (expectedClassId == 0xFFFFFFFF)
            {
                UInt32 classId = (UInt32)TLRootSerializer.Deserialize(byteList, typeof(UInt32));
                type = type.GetSubTypeWithClassId(classId);
            }
            else if (expectedClassId != 0)
            {
                UInt32 classId = (UInt32)TLRootSerializer.Deserialize(byteList, typeof(UInt32));
                if (expectedClassId != classId)
                {
                    throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "{0} =/= {1}", expectedClassId, classId));
                }
            }

            object obj = Activator.CreateInstance(type);

            type.GetTLProperties()
            .ToList()
            .ForEach(x => x.SetValue(obj, TLRootSerializer.Deserialize(byteList, x.PropertyType, x.GetCustomAttribute <TLPropertyAttribute>())));

            return(obj);
        }
예제 #2
0
        public override byte[] Serialize(object input, TLPropertyAttribute attribute)
        {
            TLObject obj = (TLObject)input;

            List <byte> list = obj.GetTLProperties()
                               .Select(x => TLRootSerializer.Serialize(x.GetValue(obj), attribute))
                               .SelectMany(x => x)
                               .ToList();

            UInt32 classId = obj.GetClassId();

            if (classId != 0)
            {
                list.InsertRange(0, TLRootSerializer.Serialize(classId));
            }

            return(list.ToArray());
        }