We have the following class...
public class ValueHolder<T>
{
public T Value { get; set; }
public Type ValueType => typeof(T);
}
...which we of course can instantiate like this.
var foo = new ValueHolder<string>() { Value = "Hello World!" };
var laa = new ValueHolder<int>() { Value = 44 };
When we serialize foo
and laa
using NewtonSoft's Json.NET, we get the following output...
// foo
{
"Value": "Hello World!",
"ValueType": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
}
// laa
{
"Value": 44,
"ValueType": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
}
The problem is deserializing it since Json.NET doesn't know it's referring to a generic so it blows up. As such, I need to write a custom converter for this class. However, I'm not sure how to instantiate a new generic instance where the data type used for 'T' in ValueHolder is stored in string-form in ValueType.
More info
I'm actually trying to Serialize/Deserialize a subclass of Dictionary<string,ValueHolder<>>
where the T of ValueHolder can be different for every instance (which of course you can't do so I'm actually subclassing Dictionary<string,object>
then putting the resolved ValueHolder into 'object') so I think the proper approach is to put the converter on the dictionary and not on ValueHolder itself.