There are such classes:

public abstract class Request { public string ToJson() => JsonConvert.SerializeObject(this); } public class LicenseRequest : Request { public string loginHash; public string passwordHash; public string macAddress; public string pluginName; public string pluginVersion; public string dllHash; } public class LicenseAnswer { public bool success; public int errorCode; public bool isOutdated; public string linkToUpdate; public string license; } public class RegistrationRequest : Request { public string login; public string password; public string macAddress; } public class RegistrationAnswer { public bool success; public int errorCode; } 

Is it possible to write a universal method or constructor for an abstract class of Answer , so that the object is initialized / populated from a JSON string?

  • For the abstract - no. Otherwise, why not? - AK
  • Here, you seem to need generalizations. Something like that public abstract class Request<T> where T : class , but it’s better not to do an abst.class, but an interface. Yes, you use public fields instead of properties intentionally? - Bulson
  • Not very intentionally) I would like to write something like public void LoadFromJson (string jsonString) {this = JsonConvert.DeserializeObject <this.GetType ()> (jsonString); } - Anton Kosenko

0