c# - 基于属性的项目之间的反射(reflection)

标签 c# reflection attributes

我有 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/

相关文章:

c# - propertyInfo.PropertyType 和 propertyInfo.GetType 之间有什么区别?

c# - 类成员的自定义属性

c# - 通过反射获取XmlRootAttribute.ElementName

python - 无法在赋值表达式中设置字段值

c# - 读取二进制文件并使用新行作为分隔符来创建二进制 block

c# - 将图像传递到 Android ListView ArrayAdapter

c# - 仅为一个通用命令处理程序注册 Autofac 装饰器

java - java.lang.reflect.Proxy 实例是否经过特殊处理以进行终结?

file - JSF 1.2 JSP 2.0 带有属性的简单自定义标记文件

c# - ClosedXML 添加带条件格式的工作表