You can create your Message type as an interface ( IMessage ), and then implement it using the GroupMessage and UserMessage .
interface IMessage { int id; //... object chat {get;} } public class GroupMessage : IMessage { public int id; public GroupChat group; public object chat {get {return group;} } } public class UserMessage : IMessage { public int id; public User user; public object chat {get {return user;} } }
But what I really do is deal with this at a higher level. If you look at the documents here, and not at a complex object that can handle all this, I would think in terms of sending or receiving a message. Then I would think about all the actions that you might want to do when you receive a message. From there, I will have an object that can raise an event for each of these actions. When you receive data (do not think of it as a message), you analyze the data and, possibly, raise several events from what was one object of the Telegram message. Some of these events may require class definitions for the data needed for the event, but now we are talking about something much narrower in scope; better suited to the principle of shared responsibility.
Going in the other direction (sending data, not receiving), the object will have methods for different types of things that you can do when sending data. Some of these methods may require classes for the required data. Whenever possible, use the same classes as for receiving events. It could even be one Send() method with many overloads.
source share