In case you can refactor a bit your code, you could use this trick. The big plus is that this is done at compile-time instead of at runtime:
public class JsonDictionary
{
public static readonly Key<int> Foo = new Key<int> { Name = "FOO" };
public static readonly Key<string> Bar = new Key<string> { Name = "BAR" };
IDictionary<string, object> _data;
public JsonDictionary()
{
_data = new Dictionary<string, object>();
}
public void Set<T>(Key<T> key, T obj)
{
_data[key.Name] = obj;
}
public T Get<T>(Key<T> key)
{
return (T)_data[key.Name];
}
public class Key<T>
{
public string Name { get; init; }
}
}
In my case I was able to replace a :
Dictionary<Type, Delegate> jsonElementDelegates = new Dictionary<Type, Delegate>
{
{ typeof(string), (Func<JsonElement, string>) (arrayItem => arrayItem.GetString()!) },
{ typeof(float), (Func<JsonElement, float>)(arrayItem => arrayItem.GetSingle()) },
{ typeof(double), (Func<JsonElement, double>) (arrayItem => arrayItem.GetDouble()) },
{ typeof(short), (Func<JsonElement, short>)(arrayItem => arrayItem.GetInt16()) },
{ typeof(ushort), (Func<JsonElement, ushort>) (arrayItem => arrayItem.GetUInt16()) },
{ typeof(int), (Func<JsonElement, int>)(arrayItem => arrayItem.GetInt32()) },
{ typeof(uint), (Func<JsonElement, uint>) (arrayItem => arrayItem.GetUInt32()) },
{ typeof(long), (Func<JsonElement, long>)(arrayItem => arrayItem.GetInt64()) },
{ typeof(ulong), (Func<JsonElement, ulong>) (arrayItem => arrayItem.GetUInt64()) },
};
With the compile-time equivalent:
public static readonly Converter<string> ConvString = new Converter<string> { Fun = arrayItem => arrayItem.GetString()! };
public static readonly Converter<float> ConvFloat = new Converter<float> { Fun = arrayItem => arrayItem.GetSingle() };
public static readonly Converter<double> ConvDouble = new Converter<double> { Fun = arrayItem => arrayItem.GetDouble() };
public static readonly Converter<short> ConvShort = new Converter<short> { Fun = arrayItem => arrayItem.GetInt16() };
public static readonly Converter<ushort> ConvUShort = new Converter<ushort> { Fun = arrayItem => arrayItem.GetUInt16() };
public static readonly Converter<int> ConvInt = new Converter<int> { Fun = arrayItem => arrayItem.GetInt32() };
public static readonly Converter<uint> ConvUInt = new Converter<uint> { Fun = arrayItem => arrayItem.GetUInt32() };
public static readonly Converter<long> ConvLong = new Converter<long> { Fun = arrayItem => arrayItem.GetInt64() };
public static readonly Converter<ulong> ConvULong= new Converter<ulong> { Fun = arrayItem => arrayItem.GetUInt64() };
public class Converter<T>
{
public Func<JsonElement, T> Fun { get; init; }
}
reference: