【问题标题】:Assign class level attribute to base class and it should be assigned to all derived classes too将类级别属性分配给基类,它也应该分配给所有派生类
【发布时间】:2020-07-09 09:50:15
【问题描述】:

我有一个自定义属性

[AttributeUsage(AttributeTargets.Class)]
    public sealed  class CustomAttribute: Attribute
    {
        public CustomAttribute()
        {
        }
    }

我在很多类中都使用过,比如

[CustomAttribute]
    public class ClassOne: MyBaseClass
    {
     .
     .

所以我有很多从 MyBaseClass 派生的类,如果我想在所有类中添加这个属性,会有很多变化。 我想要的是我将此属性添加到我的基类中,并且所有派生类也应该分配给它们相同的属性。

喜欢

[CustomAttribute]
    public class MyBaseClass
    {
     .
     .

但这并不能解决问题,我的子类仍然没有添加属性。 有什么办法可以实现吗?

【问题讨论】:

  • 如果您希望从MyBaseClass 派生的每个类都继承此属性,那么您真的需要该属性吗?毕竟,它们都继承自同一个类。

标签: c# attributes


【解决方案1】:

您应该在属性中使用[AttributeUsage(Inherited = true)] 属性。

[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class CustomAttribute : Attribute {
    public CustomAttribute() {
    }
}

但它的默认值已经是 true 并且派生类应该可用于您的自定义属性。我推荐下面的示例块以确保。

首先,定义属性。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
                AttributeTargets.Property | AttributeTargets.Field)]
public class InheritedAttribute : Attribute { }

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
                AttributeTargets.Property | AttributeTargets.Field,
    Inherited = false)]
public class NotInheritedAttribute : Attribute { }

[InheritedAttribute]
public class BaseA {
    [InheritedAttribute]
    public virtual void MethodA() { }
}

public class DerivedA : BaseA {
    public override void MethodA() { }
}

[NotInheritedAttribute]
public class BaseB {
    [NotInheritedAttribute]
    public virtual void MethodB() { }
}

public class DerivedB : BaseB {
    public override void MethodB() { }
}

然后,检查属性。

class Program {
    static void Main(string[] args) {
        Type typeA = typeof(DerivedA);
        Console.WriteLine($"DerivedA has Inherited attribute: {typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0}");
        MethodInfo memberA = typeA.GetMethod(nameof(DerivedA.MethodA));
        Console.WriteLine($"DerivedA.MemberA has Inherited attribute: {memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0}\n");

        Type typeB = typeof(DerivedB);
        Console.WriteLine($"DerivedB has NotInherited attribute: {typeB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0}");
        MethodInfo memberB = typeB.GetMethod(nameof(DerivedB.MethodB));
        Console.WriteLine($"DerivedB.MemberB has NotInherited attribute: {memberB.GetCustomAttributes(typeof(NotInheritedAttribute), true).Length > 0}");

        Console.Read();
    }
}

AttributeUsageAttribute.Inherited Property

【讨论】:

    【解决方案2】:

    问题是您如何使用该属性?因为如果你有这种情况:

     [Custom]
     public class BaseClass {}
    
     public class AClass : BaseClass {}
    

    你会的

     CustomAttribute ca = typeof(AClass).GetCustomAttributes(true).FirstOrDefault(x => x is CustomAttribute) as CustomAttribute;
    

    ca 不为空

    【讨论】:

      【解决方案3】:

      这两种解决方案都对我有用 - @ddfra 和 @gurkan 。

      我已经像这样编辑了我的代码来获取属性列表,它也可以工作。

      var attributesList = GetType().GetCustomAttributes<CustomAttribute>(inherit: true);
      

      【讨论】:

        猜你喜欢
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        • 2017-08-02
        • 2021-09-20
        • 2018-12-14
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        相关资源
        最近更新 更多