使用反射从非泛型 IEnumerable 获取列列表
Get column Lists from non-generic IEnumerable using Reflection
我有一个方法采用通用 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();
}
这里一个可能的问题是您正在传递一个 non-generic 集合,它可能是不同的对象,因此,创建 属性Info 应该在循环内以确保您可以读取 属性 来自对象。如果您确定 non-generic 集合中的所有对象都是同一类型,则可以从列表中的第一个对象(如果它至少有一个实例)读取循环外的 属性Info .
你可以试试这个,看代码注释:
public static IList GetRowValue(IEnumerable value, string propertyName)
{
// create an arraylist to be the result
var result = new ArrayList();
// loop in the objects
foreach (var item in value)
{
// search for a property on the type of the object
var property = item.GetType().GetProperty(propertyName);
// if the property was found
if (property != null)
{
// read the property value from the item
var propertyValue = property.GetValue(item, null);
// add on the result
result.Add(propertyValue);
}
}
return result;
}
查看集合中包含不同对象的工作示例:https://dotnetfiddle.net/bPgk4h
该问题与 IEnumerable 或反射无关。
数据没有填充到 IEnumerable 中,所以它进行了数千次数据库调用,我正在填充矩阵。
解决该问题后,它会立即加载。
我有一个方法采用通用 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();
}
这里一个可能的问题是您正在传递一个 non-generic 集合,它可能是不同的对象,因此,创建 属性Info 应该在循环内以确保您可以读取 属性 来自对象。如果您确定 non-generic 集合中的所有对象都是同一类型,则可以从列表中的第一个对象(如果它至少有一个实例)读取循环外的 属性Info .
你可以试试这个,看代码注释:
public static IList GetRowValue(IEnumerable value, string propertyName)
{
// create an arraylist to be the result
var result = new ArrayList();
// loop in the objects
foreach (var item in value)
{
// search for a property on the type of the object
var property = item.GetType().GetProperty(propertyName);
// if the property was found
if (property != null)
{
// read the property value from the item
var propertyValue = property.GetValue(item, null);
// add on the result
result.Add(propertyValue);
}
}
return result;
}
查看集合中包含不同对象的工作示例:https://dotnetfiddle.net/bPgk4h
该问题与 IEnumerable 或反射无关。 数据没有填充到 IEnumerable 中,所以它进行了数千次数据库调用,我正在填充矩阵。 解决该问题后,它会立即加载。