首页 > 代码库 > c#List泛型数据扩展,把List<>型数据格式化成List<SelectListItem>,用来作dropdownlist的数据

c#List泛型数据扩展,把List<>型数据格式化成List<SelectListItem>,用来作dropdownlist的数据

代码如下

        public static List<SelectListItem> CreateSelect<T>(this IList<T> t, string text, string value)
        {
            List<SelectListItem> l = new List<SelectListItem>();
            foreach (var item in t)
            {
                var propers = item.GetType().GetProperty(text);
                var valpropers = item.GetType().GetProperty(value);
                l.Add( new SelectListItem{Text=propers.GetValue(item,null).ToString(), Value=http://www.mamicode.com/valpropers.GetValue(item,null).ToString()} );>