How to cast an object to a Type extracted at runtime

前端 未结 1 1525
既然无缘
既然无缘 2021-01-02 06:59

I am using reflection to get an object\'s type, or for this issue an object that has instance properties type, at runtime and then I need to change an existing variable\'s t

相关标签:
1条回答
  • 2021-01-02 07:48

    Try Convert.ChangeType

    If exp.Split(".").Count Then
      Dim tp As Type = Nothing
      tp = x.GetType
      tp = tp.GetProperty(exp.Split(".").ElementAt(0)).PropertyType()
      'Line below works, gets the right type, and now I need both x and y values passed in to be cast to this type.
      Dim typ As Type = tp.GetType
      x = Convert.ChangeType(x, typ)
    End If
    
    0 讨论(0)
提交回复
热议问题