基于属性的项目之间的反射
本文关键字:项目 之间 反射 属性 于属性 | 更新日期: 2023-09-27 18:25:43
我有两个项目,项目A和项目B。
在项目A中,我有一个用属性Myperson装饰的类ClsPersoon。
在项目B中,我在项目A的exe文件上使用反射,我想选择具有customAttribute MyPerson的所有类型,但我在getCustomAttributes上遇到了一个错误,因为该属性在项目B.中未知
如何在没有两个项目之间的参考的情况下解决这个问题?
如果您愿意在名称上匹配,Attribute类上有一些静态方法可以获得特定目标的自定义属性:
static IEnumerable<Type> TypesWithAttribute(Assembly a, string attributeName )
{
return
a
.GetTypes( )
.Where
(
t =>
Attribute
.GetCustomAttributes( t )
.Any
(
att =>
att.GetType( ).Name == attributeName
)
);
}