Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何基于未知属性筛选对象列表_Vb.net_Linq_Datagrid_User Controls - Fatal编程技术网

Vb.net 如何基于未知属性筛选对象列表

Vb.net 如何基于未知属性筛选对象列表,vb.net,linq,datagrid,user-controls,Vb.net,Linq,Datagrid,User Controls,我创建了一个由文本框和数据网格组成的用户控件。 (以下示例中未包含代码)。 我需要按特定列过滤数据网格的内容。 列的名称取决于对象属性的名称。 对象会有所不同,但每个控件都将绑定到特定对象 我如何使用控制示例: Public Class Form1 Private ObjectList As List(Of Object) Sub New() ' This call is required by the designer. InitializeC

我创建了一个由文本框和数据网格组成的用户控件。 (以下示例中未包含代码)。 我需要按特定列过滤数据网格的内容。 列的名称取决于对象属性的名称。 对象会有所不同,但每个控件都将绑定到特定对象

我如何使用控制示例:

Public Class Form1

    Private ObjectList As List(Of Object)
    Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ObjectList = GenerateRandomData()
        AddHandler Me.MyUserControl.SelectionStatusChanged, AddressOf UpdateForm
        AddHandler Me.MyUserControl.TextChanged, AddressOf UpdateList

        Me.MyUserControl.DataSource = ObjectList
    End Sub

    Private Sub UpdateList()
        Dim FilteredList As IEnumerable(Of Object) = From obj As Item In ObjectList
                                                     Where obj.Prop_1.StartsWith(Me.MyUserControl.Text)
                                                     Select obj
        Me.MyUserControl.DataSource = FilteredList.ToList
    End Sub

    Private Sub UpdateForm()
        If Me.MyUserControl.HasItemLoaded Then
            Dim NewItem As Item = CType(Me.MyUserControl.SelectedItem, Item)
            TextBox1.Text = NewItem.Prop_1
            TextBox2.Text = NewItem.Prop_2
            TextBox3.Text = NewItem.Prop_3
            Return
        End If
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub

    Private Function GenerateRandomData() As List(Of Object)
        Randomize()
        Dim lst As New List(Of Object)
        For i = 0 To 10000
            Dim itm As New Item
            Dim value As Integer = CInt(Int((999999 * Rnd()) + 1))
            Dim value2 As Integer = CInt(Int((999999 * Rnd()) + 1))
            itm.Prop_1 = value
            itm.Prop_2 = "abc " & value & value2
            itm.Prop_3 = value2 & value & " abc"
            lst.Add(itm)
        Next
        Return lst
    End Function
End Class
我想在用户控件内处理UpdateList()的工作(移动代码或等效结果),但问题是该控件不知道对象类型,因此我无法直接从用户控件内调用Prop_1或任何属性名称

到目前为止,我是通过监听用户控制之外的事件来实现这一点的,但我想从程序员身上卸下同样多的责任


欢迎任何想法。

以下是围绕其
设置列表
方法设计的,该方法用于设置源列表(IEnumerable(Of T))和要在
更新列表
方法中过滤的属性名称。它使用反射来检索所需的属性

Public Class UserControl1
    Private list As IEnumerable
    Private filterPropInfo As Reflection.PropertyInfo

    Public Sub SetList(Of T)(list As IEnumerable(Of T), filterPropertyName As String)
        filterPropInfo = GetType(T).GetProperty(filterPropertyName, GetType(String))
        If filterPropInfo Is Nothing Then
            Throw New ArgumentException(String.Format("{0} is not a Public String Property on Type: {1}", filterPropertyName, GetType(T).FullName))
        End If
        Me.list = list
    End Sub

    Public Sub UpdateList()
        Dim FilteredList As IEnumerable(Of Object) = From obj In list
                                                     Where CStr(filterPropInfo.GetValue(obj)).StartsWith(Me.Text)
                                                     Select obj

        Me.DataGridView1.DataSource = FilteredList.ToList
    End Sub
End Class
调用
test
方法的用法示例

Public Class Form1
    Sub test()
        Dim l As New List(Of Item)
        l.Add(New Item("fred"))
        l.Add(New Item("freddy"))
        l.Add(New Item("fredrick"))
        l.Add(New Item("joe"))
        UserControl11.[Text] = "fred"
        UserControl11.SetList(l, "Field1")
        UserControl11.UpdateList()
    End Sub
End Class

Class Item
    Public Property Field1 As String
    Public Property Field2 As Int32
    Public Sub New(f1 As String)
        Me.Field1 = f1
    End Sub
End Class

非常感谢。使用反射的解决方案正是我想要的。