List<T>获取属性

public List<SelectListItem> GetDrop<T>(List<T> Dic, string KeyFormT, string ValueFormT, bool Default, string Selected,string sel, string text = null, string value = null)
{
List<SelectListItem> DItems = new List<SelectListItem>();
bool isSel = Selected == "0" ? true : false;
if (Default)
{

DItems.Add(new SelectListItem() { Text = text, Value = value, Selected = isSel });//默认项
}
var props = TypeDescriptor.GetProperties(typeof(T));
foreach (var item in Dic)
{
SelectListItem SelectItem = new SelectListItem();
SelectItem.Text = Convert.ToString(props[KeyFormT].GetValue(item));
SelectItem.Value = Convert.ToString(props[ValueFormT].GetValue(item));
string strSel = Convert.ToString(props[sel].GetValue(item));
SelectItem.Selected = strSel == Selected ? true : false;
DItems.Add(SelectItem);
}

return DItems;
}

posted @ 2014-04-02 18:27  mimo0  阅读(915)  评论(0)    收藏  举报