Reflection + LINQ
I'm truing to use reflection with LINQ on a sub that set datasource of all my ComboBox:
Usual Method:
' ACAmp Panel
cboACPanelAmp.ValueMember = "IDACAmp"
cboACPanelAmp.DisplayMember = "Description"
cboACPanelAmp.DataSource = m_Entities.ACAmps.OrderBy(Function(c As ACAmp) c.SortOrder).ToList
Want to use that sub
FillCbo(cboACPanelAmp, "ACAmp")
Private Sub FillCbo(ByVal cbo As Infragistics.Win.UltraWinEditors.UltraComboEditor, ByVal entityName As String)
cbo.ValueMember = "ID" & entityName
cbo.DisplayMember = "Description"
' need to complete this line
cbo.DataSource = GetType(RFOPSEntities).
GetProperty(entityName & "s").GetGetMethod().Invoke(m_Entities, Nothing)
' with this code
' .OrderBy(Function(c As ACAmp) c.SortOrder).ToList
' like this line
cbo.DataSource = m_Entities.ACAmps.OrderBy(Function(c As ACAmp) c.SortOrder).ToList
End Sub
I can't figure out the last part of the last line, t开发者_开发问答he LINQ
cbo.DataSource = GetType(RFOPSEntities).
GetProperty(entityName & "s").GetGetMethod().Invoke(m_Entities, Nothing)
In that line you need to tell the environment that that is a type that you can use linq on.
So I think that it is a RFOPSEntities, so something like
cbo.DataSource = DirectCast(GetType(RFOPSEntities)
.GetProperty(entityName & "s")
.GetGetMethod()
.Invoke(m_Entities, Nothing), RFOPSEntities)
Then you should be able to use your Linq OrderBy on that
cbo.DataSource = DirectCast(GetType(RFOPSEntities)
.GetProperty(entityName & "s")
.GetGetMethod()
.Invoke(m_Entities, Nothing), RFOPSEntities)
.OrderBy(Function(c As ACAmp) c.SortOrder).ToList
Thank you msarchet I modifiy a bit but it work.
cbo.DataSource = DirectCast(GetType(RFOPSEntities) _
.GetProperty(entityName & "s") _
.GetGetMethod() _
.Invoke(m_Entities, Nothing), ObjectSet(Of ACAmp)))
.OrderBy(Function(c As ACAmp)) c.SortOrder).ToList()
Now I need to do semeting like that:
cbo.DataSource = DirectCast(GetType(RFOPSEntities) _
.GetProperty(entityName & "s") _
.GetGetMethod() _
.Invoke(m_Entities, Nothing), ObjectSet(Of Type.GetType("ACAmp"))).OrderBy(Function(c As Type.GetType("ACAmp")) c.SortOrder).ToList()
The Type.GetType("ACAmp") is not goog but the type could be pass by string. How ?
精彩评论