//将实体类集合转换为支持属性名称索引的类
public static List<PropertyIndexer<T>> ConvertToPropertyIndexer<T>(List<T> dataList) {List<PropertyIndexer<T>> result = new List<PropertyIndexer<T>>();foreach (T item in dataList) {result.Add(new PropertyIndexer<T>(item));}return result;
}
//索引类转换为实体类
public static List<T> ConvertToPropertyIndexer<T>(List<PropertyIndexer<T>> dataList)
{List<T> result = new List<T>();foreach (PropertyIndexer<T> item in dataList){result.Add(item.Instance);}return result;
}
调用
public ClassA{public string name {get;set;}public string value {get;set;}
}List<ClassA> list = new List<ClassA>(){new ClassA(){name = "a",value="1" },new ClassA(){name = "b",value="2" },
}List<PropertyIndexer<ClassA>> values =DataCountCenterService.ConvertToPropertyIndexer<ClassA>(list);Console.WriteLine(values[0]["name"].ToString());//索引取字段值的返回结果是object,需要进行转换,此处输出结果为a