Silverlight 如何从覆盖的成员获取所有System.Attributes?
我试图在类的属性上查找DataMember属性。如果属性位于类中直接定义的属性上,则可以找到该属性。但是,如果该属性在基类中定义并被重写,则找不到该属性。我发现了几个链接,说明您必须使用System.Attribute来获取此信息。下面是直接引用自 当GetCustomAttributes的inherit参数为true时,在PropertyInfo或EventInfo上调用ICCustomAttributeProvider.GetCustomAttributes不会遍历类型层次结构。使用System.Attribute继承自定义属性 这似乎完全解决了使用完整框架时的问题。这似乎只解决了Silverlight中“非系统”属性的问题(在Silverlight 3.0、4.0、5.0上测试)。下面的示例代码显示,当返回另一个自定义定义的属性时,不会返回DataMemberAttribute。这是一个错误还是我遗漏了什么Silverlight 如何从覆盖的成员获取所有System.Attributes?,silverlight,reflection,Silverlight,Reflection,我试图在类的属性上查找DataMember属性。如果属性位于类中直接定义的属性上,则可以找到该属性。但是,如果该属性在基类中定义并被重写,则找不到该属性。我发现了几个链接,说明您必须使用System.Attribute来获取此信息。下面是直接引用自 当GetCustomAttributes的inherit参数为true时,在PropertyInfo或EventInfo上调用ICCustomAttributeProvider.GetCustomAttributes不会遍历类型层次结构。使用Syst
public class CustomAttribute : Attribute
{
}
public class Animal
{
[DataMember(), CustomAttribute()]
public virtual decimal Weight { get; set; }
}
public class Dog : Animal
{
public override decimal Weight { get; set; }
}
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
var animalAttributes = Attribute.GetCustomAttributes(typeof(Animal).GetProperties()[0], true);
var dogAttributes = Attribute.GetCustomAttributes(typeof(Dog).GetProperties()[0], true);
//animalAttributes contains 2 entries (DataMember, CustomAttribute)
//dogAttributes contains only 1 entry (CustomAttribute)
}
}
我认为您必须在CustomAttribute中设置AttributeUsage以接受继承: 将属性设置为true 编辑
DataMemberAttribute不允许继承 CustomAttribute正在被返回,DataMemberAttribute没有被返回。啊,对不起,我看到了相反的情况。我的错误。DataMemberAttribute不允许继承。