文字和对象属性的编程引用

时间:2010-07-15 15:03:21

标签: asp.net

我的建议是,这可能会以编程方式从一组对象属性中填充一组文字: -

For i As Integer = 1 To noOfTexts
  Dim ctl As Literal = DirectCast(FindControl("help" & i), Literal)
  If ctl IsNot Nothing Then
 ctl.Text = pageData.help(i).trim()
 ctl.Visible = True
  End If
Next

然而,行:ctl.Text = pageData.help(i).trim()失败,因为它不能理解pageData.help(i)应该转换为pageData.help1,pageData.help2等。

是否有一些语法可以在VS2010 Asp.net VB中实现?

1 个答案:

答案 0 :(得分:2)

您可以使用reflection ...

动态引用属性
 Dim PageDataType As Type = GetType(pageData)
 Dim PropertyName As String = "help" & i
 Dim Property As PropertyInfo = PageDataType.GetProperty(PropertyName)
 Dim PropertyValue As String

 If Property IsNot Nothing
     PropertyValue = Property.GetValue(pageData, Nothing)
 End If

注意:Reflection in .NET is slower than direct access,但不足以使其使用不切实际