Faster version of Convert.ChangeType

前端 未结 5 1390
北荒
北荒 2021-01-02 04:12

In an application that I have, I am doing quite frequent calls to Convert.ChangeType in order to convert a value to a dynamically loaded type.

However

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-02 04:38

    I know this sounds crazy but, you can use Newtonsoft JSON.NET to convert your object into a serialized string and then cast from that string into any other type.

    var jsontemp = JsonConvert.SerializeObject(anyObject);
    var ConvertedObject = JsonConvert.DeserializeObject(jsontemp, desiredType);
    

提交回复
热议问题