我有 2 个项目,项目 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
)
);
}
关于c# - 基于属性的项目之间的反射(reflection),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34108872/