2011-06-21 23 views
0

私は、オブジェクトのリストの内容で仮想リストビューを設定しています。これは、。NET 3.5で実行されているwinforms listviewコントロールです。私は動的にオブジェクトのパブリックプロパティから列を生成しています。リストをソートするためにプロパティに動的にアクセスする最も良い方法は何ですか?

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ハンドラ内のオブジェクトのリストをソートする必要があります。

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が生成されます。このために私は、フォームのコンストラクタでループを使用していましたその列のプロパティーのタイプをチェックして列をクリックします。

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... 

これは明らかに同じデータ型を含む列のコードを複製します。各列を処理し、その後else文(またはswitch文)があればこれを行うには非動的な方法は、おそらく長いを書くことであろう。私は、列の並べ替え方法を決定するためにプロパティタイプを使用するコードでこれを置き換えている:これは正常に動作している

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); 
    }); 
} 

を、私は遅くなることがリフレクションを使用してそのパフォーマンスを読んだことがあるし、行うには良い方法があることこの。コードを改善したいと思います。未知のオブジェクトプロパティを実行時に動的にソートするにはどうすればよいですか?

答えて

0

列名を取得できる場合は、動的LINQ(http://aonnull.blogspot.com/2010/08/dynamic-sql-like-linq-orderby-extension.html)を使用してください。

関連する問題