自定义的属性继承抽象属性 [英] Inheritance of Custom Attributes on Abstract Properties
问题描述
我有一个自定义属性,我想申请我的基抽象类,这样我可以跳过HTML显示项时不需要用户观看的元素。似乎重写基类中的属性不继承的属性。
请问压倒一切的基本属性(抽象的或虚拟的)吹走放在原来的物业属性?
从属性类认定中的
[AttributeUsage(AttributeTargets.Property,
继承= TRUE,
的AllowMultiple = FALSE)]
公共类NoHtmlOutput:属性
{
}
从抽象类认定中的
[NoHtmlOutput]
公共抽象的Guid的UniqueID {搞定;组; }
从具体类认定中的
公众覆盖的Guid的UniqueID {{返回MasterId;}集合{MasterId =值;}}
从类检查的属性。
键入T = o.GetType();
的foreach(PI的PropertyInfo在t.GetProperties())
{
如果(pi.GetCustomAttributes(typeof运算(NoHtmlOutput),TRUE)。长度== 1)
继续;
//处理逻辑到这里
}
没有,属性继承。
这是 GetCustomAttributes不看父声明()
方法。它仅着眼于应用到指定的成员属性。 从文档:
备注
这个方法忽略了继承
参数作为属性和事件。
为了寻找继承链
属性的属性和事件,
使用的适当重载
属性.. ::。GetCustomAttributes
方法。
块引用>I've got a custom attribute that I want to apply to my base abstract class so that I can skip elements that don't need to be viewed by the user when displaying the item in HTML. It seems that the properties overriding the base class are not inheriting the attributes.
Does overriding base properties (abstract or virtual) blow away attributes placed on the original property?
From Attribute class Defination
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)] public class NoHtmlOutput : Attribute { }
From Abstract Class Defination
[NoHtmlOutput] public abstract Guid UniqueID { get; set; }
From Concrete Class Defination
public override Guid UniqueID{ get{ return MasterId;} set{MasterId = value;}}
From class checking for attribute
Type t = o.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { if (pi.GetCustomAttributes(typeof(NoHtmlOutput), true).Length == 1) continue; // processing logic goes here }
解决方案No, attributes are inherited.
It's the
GetCustomAttributes()
method that does not look at parent declarations. It only looks at attributes applied to the specified member. From the docs:Remarks
This method ignores the inherit parameter for properties and events. To search the inheritance chain for attributes on properties and events, use the appropriate overloads of the Attribute..::.GetCustomAttributes method.
这篇关于自定义的属性继承抽象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!