【发布时间】:2019-05-09 20:20:36
【问题描述】:
我有一个方法,它采用通用 IEnumerable 并为每列生成唯一值列表。不用说这很慢,我猜这是由于使用了所有反射。 下面是一些示例代码:
private void PopulateReferenceMatrices(IEnumerable newValue)
{
Type t = newValue.GetType();
Type baseType = t.GetGenericArguments()[0];
PropertyInfo[] properties;
Dictionary<string, int> indexValues = new Dictionary<string, int>();
properties = baseType.GetProperties();
int numProperties = properties.Count();
ListValues = new List<object>[numProperties];
for (int i = 0; i < numProperties; i++)
{
indexValues.Add(properties[i].Name, i);
FilterValues[i] = new List<object>();
}
//populate values into array
foreach (dynamic da in newValue)
{
foreach (PropertyInfo d in properties)
{
Object property = d.GetValue(da);
ListValues[indexValues[d.Name]].Add(property);
}
}
}
我可以为每个属性生成一个值列表,而无需逐行遍历 IEnumerable 并将每个属性转换为对象吗?
是否有更快的方法来为 IEnumerable 中的每个项目执行类似的操作?:
public IList getRowValue(IEnumerable value, string propertyName)
{
value.Select(x => x.propertyName).ToList();
}
【问题讨论】:
-
你用反射做什么,你一般想完成什么?
-
这是一个自定义的datagrid控件,每个headerColumn中都有一个列表框来过滤网格中的数据。 ListValues 是在这些列表框中选中或取消选中的值。由于我无法知道在运行时传递到自定义数据网格中的类型,因此我必须即时弄清楚。
-
你为什么使用
dynamic da?GetProperty是什么?为什么你循环PropertyInfo对象只是为了将d.Name传递给方法? -
我认为动态可能会加快速度(文档说它把大多数东西都当作对象对待)。我认为可能有一种方法可以更轻松地从动态引用中获取属性。 GetProperty 是对 CompilerServices.Versioned.CallByName 函数的调用,因为有人写过它更快。不是(我把它改回GetValue(da)。关于属性,第一次主要是创建一个字典来快速访问一个索引,另外一次我多次使用它。
-
a是干什么用的?获取整数的顺序列表似乎是一种非常复杂的方法,就像通过字典查找来获取整数一样。for (int j1 = 0; j1 < numProperties; ++j1) ListValues[j1].Add(properties[j1].GetValue(da));(还有int numProperties = properties.Length;)呢?另外,为什么这不是IEnumerable<T>的通用方法?
标签: c# list ienumerable