我正在调整使用反射和LINQ在一个sub上设置我所有ComboBox的数据源:
常用方法:
' ACAmp Panel
cboACPanelAmp.ValueMember = "IDACAmp"
cboACPanelAmp.DisplayMember = "Description"
cboACPanelAmp.DataSource = m_Entities.ACAmps.OrderBy(Function(c As ACAmp) c.SortOrder).ToList想用那个潜艇吗?
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我搞不懂最后一行的最后一部分,LINQ
发布于 2011-05-12 23:21:09
cbo.DataSource = GetType(RFOPSEntities).
GetProperty(entityName & "s").GetGetMethod().Invoke(m_Entities, Nothing)在这一行中,您需要告诉环境这是一种可以使用linq的类型。
所以我认为它是一个RFOPSEntities,所以类似于
cbo.DataSource = DirectCast(GetType(RFOPSEntities)
.GetProperty(entityName & "s")
.GetGetMethod()
.Invoke(m_Entities, Nothing), RFOPSEntities)然后你就可以在上面使用Linq OrderBy了。
cbo.DataSource = DirectCast(GetType(RFOPSEntities)
.GetProperty(entityName & "s")
.GetGetMethod()
.Invoke(m_Entities, Nothing), RFOPSEntities)
.OrderBy(Function(c As ACAmp) c.SortOrder).ToList发布于 2011-05-12 23:50:24
谢谢你,msarchet,我做了一点修改,但它是有效的。
cbo.DataSource = DirectCast(GetType(RFOPSEntities) _
.GetProperty(entityName & "s") _
.GetGetMethod() _
.Invoke(m_Entities, Nothing), ObjectSet(Of ACAmp)))
.OrderBy(Function(c As ACAmp)) c.SortOrder).ToList()现在我需要这样做:
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()Type.GetType("ACAmp")不是goog,但类型可以通过字符串传递。怎么做到的?
https://stackoverflow.com/questions/5969471
复制相似问题