Faster version of Convert.ChangeType

前端 未结 5 1393
北荒
北荒 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:22

    I haven't tested if faster, but this is an alternative way for dynamic casting. This is alsp more universal, since Convert.ChangeType() has some limitations, as you have seen (Guids, Nullable types)

    value = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(str);
    

提交回复
热议问题