如何获取属于自定义属性的属性?

我需要在自定义属性中找到应用自定义属性的属性的类型。

例如:

[MyAttribute] string MyProperty{get;set;} 

给定MyAttribute的实例,我如何获得MyProperty的Type描述符?

换句话说,我正在寻找System.Type.GetCustomAttributes()的反面

属性本身对用它装饰的对象一无所知。 但是,您可以在重新检索属性时注入此信息。
在某些时候,您必须使用类似于以下的代码检索属性。

 PropertyInfo propertyInfo = typeof(MyType).GetProperty("MyProperty"); Object[] attribute = propertyInfo.GetCustomAttributes(typeof(MyAttribute), true); if (attribute.Length > 0) { MyAttribute myAttribute = (MyAttribute) attributes[0]; // Inject the type of the property. myAttribute.PropertyType = propertyInfo.PropertyType; // Or inject the complete property info. myAttribute.PropertyInfo = propertyInfo; } 

自定义属性对属性元素一无所知,因此除非您枚举系统中的所有类型并检查它们是否包含此类属性,否则我不认为您想要做什么。