Reputation: 2224
I am trying to deserialize an object from the database, the object is stored in a column as a json and then de-serialized, the thing is that I am trying to make a generic method and to achieve that, so I also store in a column the deserialization type.
So what I want to do is to get the type through Type.GetType("mytype")
and pass that type to a method where it is essential that you pass the type.
The problem is that for some reason visual studio does not understand this systax and highlights it as an error, or maybe I am using a bad approach, what I do is :
string toDeserialize = "jsonObject";
JsonConvert.DeserializeObject<Type.GetType("customType")>(toDeserialize);
And the error that visual studio throws is :
Operator '<' cannot be applied to operands of type 'method group' and 'System.Type'
What can I do to solve this?
Upvotes: 0
Views: 73
Reputation: 149020
Generic type parameters provided in this way must be known at compile time. If you want to call this generic method with using a type specified at run-time, you could use reflection to generate a the method via MakeGenericMethod
.
But thankfully, JsonConvert
provides this non-generic overload as an alternative:
Type resultType = Type.GetType("customType");
object result = JsonConvert.DeserializeObject(toDeserialize, resultType);
Upvotes: 2