I am trying to get the current class type, from a base class, and pass it as a parameter.
The code should do something like this:
public BaseClass
{
public BaseClass()
{
var MyType = GetType();
var A = JsonConvert.DeserializeObject<MyType>(...);
}
}
so if it is instanced like this:
public class MyObject : BaseClass
then the last call would do:
var A = JsonConvert.DeserializeObject<MyObject>(...);
but I can't find how to pass the type as a parameter.
any idea how this can be achieved?
You can use JsonConvert.DeserializeObject(string value, Type type)
. It allows you to specify the type of the object you want to build.
public BaseClass
{
public BaseClass()
{
var MyType = GetType();
var A = JsonConvert.DeserializeObject("{ json blob }", MyType);
}
}
A
will be of type MyType
. JsonConvert.DeserializeObject
is returning an object
you will have to cast the result to what you want at some point.
The documentation is available here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.