Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
C# 动态访问属性对列表进行排序的最佳方法是什么?_C#_List_Sorting_Reflection_Properties - Fatal编程技术网

C# 动态访问属性对列表进行排序的最佳方法是什么?

C# 动态访问属性对列表进行排序的最佳方法是什么?,c#,list,sorting,reflection,properties,C#,List,Sorting,Reflection,Properties,我正在用对象列表的内容填充虚拟listview。它是一个winforms listview控件,运行在.Net 3.5上。我正在从对象的公共属性动态生成列。为此,我在表单的构造函数中使用了一个循环: properties = typeof(MyCustomObject).GetProperties().ToArray(); foreach (PropertyInfo property in properties) { ColumnHeader ch = new ColumnHeader(

我正在用对象列表的内容填充虚拟listview。它是一个winforms listview控件,运行在.Net 3.5上。我正在从对象的公共属性动态生成列。为此,我在表单的构造函数中使用了一个循环:

properties = typeof(MyCustomObject).GetProperties().ToArray();
foreach (PropertyInfo property in properties)
{
    ColumnHeader ch = new ColumnHeader();
    ch.Text = property.Name;
    listView1.Columns.Add(ch);
}
我在listView1_RetrieveVirtualItem处理程序中生成listviewitems:

MyCustomObject myCustomObject = myCustomObjects[e.ItemIndex];
ListViewItem item = new ListViewItem(myCustomObject.ID, 0);
foreach (PropertyInfo property in properties)
{
    var propvalue = property.GetValue(myCustomObject, null);
    if (propvalue == null)
        item.SubItems.Add("");
    else
        item.SubItems.Add(propvalue.ToString());
}
我需要在单击列时,通过检查该列的属性类型,对listView1_ColumnClicked处理程序中的对象列表进行排序。实现这一点的非动态方式可能是编写一个长if-then-else语句或一个switch语句来处理每一列:

if (sortColumn == 1)
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        DateTime t1 = o1.FirstDate ?? DateTime.MinValue;
        DateTime t2 = o2.FirstDate ?? DateTime.MinValue;
        return t1.CompareTo(t2);
    });
}
else if (sortColumn == 2)
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        DateTime t1 = o1.SecondDate ?? DateTime.MinValue;
        DateTime t2 = o2.SecondDate ?? DateTime.MinValue;
        return t1.CompareTo(t2);
    });
}
else if (sortColumn == 3)
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        return e1.FirstName.CompareTo(e2.FirstName);
    });
}
else if (sortColumn == 4)
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        return e1.LastName.CompareTo(e2.LastName);
    });
}
else
    // and so on, for each property...
这显然与包含相同数据类型的列的代码重复。我已将其替换为使用属性类型确定列排序方式的代码:

PropertyInfo property = properties[sortColumn];
Type type = property.PropertyType;
if (type == typeof(DateTime))
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        DateTime t1 = (DateTime)property.GetValue(o1, null);
        DateTime t2 = (DateTime)property.GetValue(o2, null);
        return t1.CompareTo(t2);
    });
}
else if (type == typeof(int))
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        int n1 = (int)property.GetValue(o1, null);
        int n2 = (int)property.GetValue(o2, null);
        return n1.CompareTo(n2);
    });
}
else if (type == typeof(string))
{
    myCustomObjects.Sort(delegate(MyCustomObject o1, MyCustomObject o2)
    {
        string s1 = (string)property.GetValue(o1, null);
        string s2 = (string)property.GetValue(o2, null);
        return s1.CompareTo(s2);
    });
}

这很好,但我已经读到,使用反射的性能可能会很慢,而且有更好的方法可以做到这一点。我想改进我的代码。如何在运行时动态访问未知对象属性以对其进行排序?

如果可以获得列名,请尝试使用动态LINQhttp://aonnull.blogspot.com/2010/08/dynamic-sql-like-linq-orderby-extension.html