lundi 21 août 2017

How to extract the class JObject represent in json.net?

I'm deserializing a JSON string to a type that's not known in compile time, so I made use of the overloaded JsonConvert.DeserializeObject method in json.net library:

    public static object GetJsonType(string json, Type type)
    {
        var logObject = JsonConvert.DeserializeObject(json, type);
        return logObject;
    }

I call it like so:

Type jsonType = Type.GetType(log.JSONClassName); 
var obj = AuditHelper.GetJsonType(log.ObjectJson, jsonType);

and then I create an instance of a WPF window :

var formType = Type.GetType("DiERP." + log.FormName);
var form = Activator.CreateInstance(formType, obj);

and this is the constructor:

public FrmControlCenters(object jControlCenters)
{
    //can't cast it to the class it represent:
    //var ob = (JClsTbControlCenters)jControlCenters;

}

in this constructor I know the type of the jControlCenters , the problem is that I can't cast it to that type. I'm pretty know a little about the JObject (the type passed to the constructor) , but can't use it .. so my question how to get the type I want from it?





Aucun commentaire:

Enregistrer un commentaire