相关疑难解决方法(0)

如何将对象强制转换为在运行时提取的Type

我使用反射来获取对象的类型,或者对于此问题,在运行时使用具有实例属性类型的对象,然后我需要将现有变量的类型更改为新找到的类型.这可能吗?例如,下面的代码并没有在内部指示的行工作:

Public Sub DoSomething(x As T, y As T, exp As String)

'This is a instance property on the object of a different type
'i.e. 'T.AnotherType' We need to reflect to find out what type of object
'AnotherType is and work with it
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 …
Run Code Online (Sandbox Code Playgroud)

.net c# vb.net reflection

9
推荐指数
1
解决办法
8781
查看次数

标签 统计

.net ×1

c# ×1

reflection ×1

vb.net ×1