Using System.Type as at runtime when deserializing with Json.Net

后端 未结 1 760
面向向阳花
面向向阳花 2020-12-04 02:48

I have a process that needs to be able to call on a function in the following manner.

public static Task Convert(string payload, Type type)
{
          


        
相关标签:
1条回答
  • 2020-12-04 03:38

    You do not need to jump through hoops. JsonConvert.DeserializeObject has a non-generic overload that accepts a Type.

    public static Task<string> Convert(string payload, Type type)
    {
        JsonSerializerSettings settings = new JsonSerializerSettings().Configure();//<--- Pull in extension methods to handle custom types
        return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(payload, type), settings));
    }
    
    0 讨论(0)
提交回复
热议问题