System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(typeof(CustomAttribute));
System.Type[] types = asm.GetExportedTypes();
Func<Attribute[], bool> IsMyAttribute = o =>
{
foreach(Attribute a in o)
{
if (a is CustomAttribute)
return true;
}
return false;
};
System.Type[] cosType = types.Where(o =>
{
return IsMyAttribute(System.Attribute.GetCustomAttributes(o,true));
}
).ToArray();
cosType数组即为所有被CustomAttribute标记过的类。
本文介绍如何使用C#反射技术,通过System.Reflection命名空间的方法,筛选出所有被特定自定义属性CustomAttribute标记的类。代码示例展示了如何定义筛选逻辑,使用LINQ查询表达式从程序集中获取这些类。
525

被折叠的 条评论
为什么被折叠?



