2010-07-15 49 views
2

我有意見,這可能對編程從一組對象的屬性填充一組文字的工作: -文字和對象屬性的程序化參考

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。幫助(i).trim()失敗,因爲它不明白,pageData.help(i)應該翻譯爲pageData.help1,pageData.help2等

是否有一些語法,將在VS2010中實現此Asp.net VB?

+0

什麼是'pageData'? – 2010-07-15 15:16:01

+0

pageData是一個對象 – Craig 2010-07-17 09:56:15

回答

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,但還不足以使它的使用不切實際