我需要从自定义属性中找到应用自定义属性的属性的类型。如何获取属于自定义属性的属性?
例如:
[MyAttribute]
string MyProperty{get;set;}
鉴于MyAttribute的情况下,我怎么能得到myProperty的一个类型描述符?
换句话说,我要找System.Type.GetCustomAttributes()
我需要从自定义属性中找到应用自定义属性的属性的类型。如何获取属于自定义属性的属性?
例如:
[MyAttribute]
string MyProperty{get;set;}
鉴于MyAttribute的情况下,我怎么能得到myProperty的一个类型描述符?
换句话说,我要找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;
}
心灵读书满分:-) – 2009-05-27 17:29:23
自定义属性一无所知归因元素,所以我不认为你想要什么是可以做到的,除非你的对面枚举系统中的所有类型并检查它们是否包含此类属性。
你可能最好说你正在做什么,因为你所问的是不可能的。 – 2009-05-27 17:12:16