mercredi 14 mars 2018

Deserialize string by class name

Let's say I have a Value that is deserialized from a class.

public class MyValue
{
    public string MyPropertyA { get; set; }
    public string MyPropertyB { get; set; }
    public string DeserializationClass { get; } = typeof(MyValue).Name;
}

I serialize this using JsonConvert class. MyValue class has a property DeserializationClass that should be used as info from which class the string was serialized from. In other words, when I deserialize the string into an object, this property serves as info which class should be used to deserialize the string. However, I am kinda stuck here as I am not sure how to get back the class from the string. Can anybody help me here?

public class Program
{
    void Main()
    {
        var objValue = new MyValue { MyPropertyA="Something", MyPropertyB="SomethingElse" };
        var serialized = JsonConvert.SerializeObject<MyClass>(value);
        //How to deserialize based on 'DeserializationClass' property in serialized string?
        var deserialized = JsonConvert.Deserialize<???>(serialized);
    }
}





Aucun commentaire:

Enregistrer un commentaire