2015-01-06 96 views
1

我有一個對象列表。我想要動態地在一個字段中獲取所有值。 我解釋更多。我讀了我的動態對象的所有屬性。當我找到一個圖像字段時,我想獲取我的對象列表中的所有圖像。從Linq的動態字段獲取值

For Each col In Ret.StructTbl.LstCol 
     If col.EstImage Then 
      col.Liste = GetType(List(Of Eleve)).GetProperty(col.SQLName).GetValue(Ret.LstDatas.LstObj, Nothing) 
     End If 
    Next 

我用它(vb.net),但我

拉提及此戰爭D'OBJET n'est PASdéfinieàUNE例如科特迪瓦聯合國OBJET

我認爲這是因爲我有一些空值或者我採取了錯誤的方式? 它的後續filter and sorter dynamic null value

我eleve Stucture

Public Class Eleve 

<TableFullOption.TFOAttribute("Key")> Property Id As Integer 
<TableFullOption.TFOAttribute("Image")> Property Img As String 
Property Name As String 
Property Major As Boolean 
<TableFullOption.TFOAttribute("FK_Sexe")> Property Sex As Integer 
Property English As Nullable(Of Integer) 
Property Japanese As Nullable(Of Double) 
Property Calculus As Decimal 
Property Geometry As Integer 
End Class 

和col.SQLName = 「圖」

你能不能幫我... 我要讓所有的圖像我的列表've(mypicture.jpg,img1.png ...)

+0

哪種語言是什麼?我可以把它解釋爲「對象引用錯誤」嗎? –

+0

代碼在vb.net – YannickIngenierie

+0

不是代碼,我的意思是異常消息。 –

回答

1

This GetProperty() statement returns Nothing,因爲在類型List(T)上找不到屬性名稱。

GetType(List(Of Eleve)).GetProperty(col.SQLName) = Nothing ' true 

唯一的例外是,在未來的呼叫提升到GetValue(),因爲你基本上是呼籲空引用的方法。

Nothing.GetValue(...) 

你打算找上Eleve型,而不是財產?

GetType(Eleve).GetProperty(col.SQLName) 
+0

你寫...但是當我嘗試之前尋求幫助,我錯誤導致我在Eleve列表上搜索:GetValue(Ret.LstDatas.LstObj,Nothing)我有消息:對象不符合目標類型 – YannickIngenierie

0

我覺得...... 首先我創建了一個智能功能

Public Shared Function GetColumn(Of MaClass As Class)(ByVal items As List(Of MaClass), ByVal columnName As String) As List(Of String) 
    Return items.Select(Function(x) If(x.GetType().GetProperty(columnName).GetValue(x, Nothing), String.Empty).ToString).ToList 
End Function 

我呼籲像

For Each col In Ret.StructTbl.LstCol 
    If col.EstImage Then 
     col.Liste = GetColumn(Of Eleve)(Ret.LstDatas.LstObj, col.SQLName) 
    End If 
Next