TLSharp/TeleSharp.TL/TLObject.cs

51 lines
1.2 KiB
C#
Raw Normal View History

2016-09-24 15:38:26 +02:00
using System;
using System.IO;
namespace TeleSharp.TL
{
2017-12-20 12:06:31 +01:00
public abstract class TLObject
2016-09-24 15:38:26 +02:00
{
2017-12-20 12:06:31 +01:00
public abstract int Constructor { get; }
public void Deserialize(BinaryReader reader)
2016-09-24 15:38:26 +02:00
{
2017-12-20 12:06:31 +01:00
int constructorId = reader.ReadInt32();
if (constructorId != Constructor)
throw new InvalidDataException("Constructor Invalid");
DeserializeBody(reader);
2016-09-24 15:38:26 +02:00
}
public abstract void DeserializeBody(BinaryReader br);
2017-12-20 12:06:31 +01:00
2016-09-24 15:38:26 +02:00
public byte[] Serialize()
{
using (MemoryStream m = new MemoryStream())
using (BinaryWriter bw = new BinaryWriter(m))
{
Serialize(bw);
bw.Close();
m.Close();
return m.GetBuffer();
}
}
2017-12-20 12:06:31 +01:00
2016-09-24 15:38:26 +02:00
public void Serialize(BinaryWriter writer)
{
writer.Write(Constructor);
SerializeBody(writer);
}
2017-12-20 12:06:31 +01:00
public abstract void SerializeBody(BinaryWriter bw);
}
public class TLObjectAttribute : Attribute
{
public TLObjectAttribute(int Constructor)
2016-09-24 15:38:26 +02:00
{
2017-12-20 12:06:31 +01:00
this.Constructor = Constructor;
2016-09-24 15:38:26 +02:00
}
2017-12-20 12:06:31 +01:00
public int Constructor { get; private set; }
2016-09-24 15:38:26 +02:00
}
}