抽象属性上自定义属性的继承 [英] Inheritance of Custom Attributes on Abstract Properties

查看:28
本文介绍了抽象属性上自定义属性的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义属性,我想将其应用于我的基本抽象类,以便在以 HTML 显示项目时可以跳过不需要用户查看的元素.似乎覆盖基类的属性没有继承属性.

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?

来自属性类定义

[AttributeUsage(AttributeTargets.Property,
                Inherited = true,
                AllowMultiple = false)]
public class NoHtmlOutput : Attribute
{
}

来自抽象类定义

[NoHtmlOutput]
public abstract Guid UniqueID { get; set; }

来自具体类定义

public override Guid UniqueID{ get{ return MasterId;} set{MasterId = value;}}

从类检查属性

        Type t = o.GetType();
        foreach (PropertyInfo pi in t.GetProperties())
        {
            if (pi.GetCustomAttributes(typeof(NoHtmlOutput), true).Length == 1)
                continue;
            // processing logic goes here
        }

推荐答案

不,属性是继承的.

GetCustomAttributes() 方法不查看父声明.它只查看应用于指定成员的属性.来自文档:

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:

备注

该方法忽略继承用于属性和事件的参数.搜索继承链属性和事件的属性,使用适当的重载属性..::.GetCustomAttributes方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆