我的建议是,这可能会以编程方式从一组对象属性中填充一组文字: -
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中实现?
答案 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,但不足以使其使用不切实际