您可以将其添加到查询中:
where type.GetCustomAttribute<GenericConfigAttribute>().MyEnum == GenericConfigType.Type1
例如:
var type1Types =
from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsDefined(typeof(GenericConfigAttribute), false)
where type.GetCustomAttribute<GenericConfigAttribute>().MyEnum
== GenericConfigType.Type1
select type;
或稍微简化(更安全):
var type1Types =
from type in Assembly.GetExecutingAssembly().GetTypes()
where type.GetCustomAttribute<GenericConfigAttribute>()?.MyEnum
== GenericConfigType.Type1
select type;
如果需要处理同一类型上的多个属性,可以执行以下操作:
var type1Types =
from type in Assembly.GetExecutingAssembly().GetTypes()
where type.GetCustomAttributes<GenericConfigAttribute>()
.Any(a => a.MyEnum == GenericConfigType.Type1)
select type;