我像这样过滤PropertyInfo
列表:
foreach (PropertyInfo propertyInfo in
ClassUtils.GetProperties(patternType).
Where(pi => pi.GetCustomAttribute<TemplateParamAttribute>() != null).
OrderBy(pi1 => pi1.GetCustomAttribute<TemplateParamAttribute>().Order))
{
TemplateParamAttribute attr = propertyInfo.GetCustomAttribute<TemplateParamAttribute>();
...
这工作正确,但我不满意每次迭代中3个GetCustomAttribute
调用。是否有办法减少GetCustomAttribute
调用的数量(仍然使用linq)?
是否有办法减少GetCustomAttribute调用的数量(仍然使用linq)?
绝对-尽早执行投影。为了可读性,我会在foreach循环的之前声明的查询中这样做:
var query = from property in ClassUtils.GetProperties(patternType)
let attribute = property.GetCustomAttribute<TemplateParamAttribute>()
where attribute != null
orderby attribute.Order
select new { property, attribute };
foreach (var result in query)
{
// Use result.attribute and result.property
}
您可以使用以下查询,它使用声明匿名类型的let
关键字。
var select = from propertyInfo in typeof (ClassUtils).GetProperties(patternType)
let attr = propertyInfo.GetCustomAttribute<TemplateParamAttribute>()
where attr != null
orderby attr.Order
select propertyInfo;
foreach (var propertyInfo in select)
{
//Operate
}
如何:
foreach(TemplateParamAttribute attr in
ClassUtils.GetProperties(patternType).
Select(pi => pi.GetCustomAttribute<TemplateParamAttribute()).
Where(pa => pa != null).
OrderBy(pa.Order))
{
// Use attr
}
如果你需要在循环中同时访问属性和属性,你可以这样做:
foreach(var info in
ClassUtils.GetProperties(patternType).
Select(pi => new {prop = pi, attr = pi.GetCustomAttribute<TemplateParamAttribute()}).
Where(pia => pia.attr != null).
OrderBy(pia.attr.Order))
{
// Use info.prop and info.attr
}
如果您的propertyInfo
对象没有使用GetCustomAttribute
以外的任何内容:
foreach (TemplateParamAttribute attr in ClassUtils.GetProperties(patternType)
.Select(pi => pi.GetCustomAttribute<TemplateParamAttribute>())
.Where(a => a != null)
.OrderBy(a => a.Order))
{
}
为了避免多次调用GetCustomAttribut
,您可以为查询引入新的范围变量tpa
。如果在循环中只需要属性,那么获取自定义属性并遍历它们:
var attributes = from pi in ClassUtils.GetProperties(patternType)
let tpa = pi.GetCustomAttribute<TemplateParamAttribute>()
where tpa != null
orderby tpa.Order
select tpa;
foreach(TemplateParamAttribute attr in attributes)
{
// ...
}