Overwrite customattribute on derived class

后端 未结 3 1261
心在旅途
心在旅途 2021-01-08 00:52

We have a custom attribute

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class CustomDesignerAttribute: Attribute

the

相关标签:
3条回答
  • 2021-01-08 01:33

    I think it is possible in the following way:

    1. Using TypeDescriptor

    In your CustomDesignerAttribute overwrite TypeId:

    public override object TypeId
    {
         get
         {
             return Key.GetHashCode();
          }
     }
    

    The base implementation of TypeId just uses the attribute type, so no parameters would be involved.

    Then u can use TypeDescriptor.GetAttributes(typeof(ChildClass)).OfType<CustomDesignerAttribute>()

    TypeDescriptor (in contrast to GetType().GetCustomAttributes) returns only one attribute based on the same TypeId. I tested it and it is the most derived attribute matching the TypeId that is returned.

    So if your TypeId represents the key of your attribute then you can overwrite it on derived classes - when using TypeDescriptor to get the attribute! Note that still multiple attributes are possible as long as they differ in their key.

    Note: TypeDescriptor also finds dynamically added attributes (added at runtime)

    2. Using Remove Property

    You could add a public bool Remove { get; set; } to your CustomDesignerAttribute. You can set it to true in your derived class while setting the other parameters identical to the base class attribute you want to remove. Then add another attribute with same key but your desired value to your derived class. When getting the attributes you have to evaluate the Remove property in a smart manner. Either using TypeDescriptor as in 1) with TypeId e.g. returning Key.HashCode() + Value.GetHashCode() or using GetType().GetCustomAttributes, in both ways you have to loop through the list of attributes and filter. You have to be aware in what order these lists are, if most derived types first or the other way around.

    0 讨论(0)
  • 2021-01-08 01:47

    use [AttributeUsage(Inherited=false)] to prevent the attribute being inherited by the derived class.

    0 讨论(0)
  • 2021-01-08 01:48

    No, it's not possible to override the existing attribute.

    Attributes are metadata attached to an object (assembly, class, method, variable and so on) so they always keep this connection.

    If you want to give a default "behavior" in the base class and override it in some derived classes you have to check all the attributes returned by GetCustomAttributes() to use only the most derived one (the first in the list).

    0 讨论(0)
提交回复
热议问题