lundi 19 octobre 2020

How can i deserialize a generic object in order to call a method on it without knowing the object's type

I have a serialized json object and access to its fully qualified name ("Foo.Bar" for example) and I would like to deserialize it in a generic way. I would like to do something like this:

var deserialized = JsonConvert.DeserializeObject<Type.GetType("Foo.Bar")>(json);

I know it is not possible, and I've stumbled upon this answer which hint of a possible way to solve my problem using reflection.

For some context, when there is an exception raised in my project I serialize the arguments of the method in which the exception is being raised and also the instance of the object on which the method is being been called. Finally, I get the fully qualified name of the instance and the method's name and also store them. I want to deserialize the instance in a generic way in order to call the method on it and go through a faulty execution flow (and help me debug my applications.).

I don't want to use a switch case scenario because it would require too much work to keep it up to date. I am aware that the serialization process might fail in the first place. I'm open to other solution that fit the context aforementioned, but I would also like to know if there is a way to do what I wanted to do in the snippet.





Aucun commentaire:

Enregistrer un commentaire