I have an issue with deserializing a class that has method within it that should be triggered once the object is deserialized. For the purpose of this question i will simplify the problem as much as possible.
public class Cat:IAnimal
{
public string CatBreed{get;set;}
public void Sounds()
{
console.log("Meow")
}
}
public class Dog:IAnimal
{
public string DogBreed{get;set;}
public void Sounds()
{
console.log("Woof");
}
}
public interface IAnimal
{
void Sounds();
}
So somewhere along the lines both classes are being instantiated and serialized and being forwarded to for deserialization in a same place, where it's not important which class it is we only need to trigger Sounds method. So this didn't work because instance of the interface cannot be created
JsonConvert.DeserializeObject<IAnimal>(json)
But since i know that the arriving objects will surely have method Sounds() i tried converting it into dynamic or plain Object which led to discovery that JsonConvert is converting the objects and dynamics into JObjects, and the method Sounds cannot be invoked because JObject has no definition for it. If you provide specific class to the DeserializeObject, the method will exists and work as intended but for the purpose of being completely generic i need to invoke it in this or similar manner.
Is there a way of achieving this?