Overwrite customattribute on derived class

北战南征 提交于 2019-12-19 05:16:22

问题


We have a custom attribute

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

then we have a base class decorated with this attribute

[CustomDesigner(someKey, someValue)]
public class BaseClass

then we have a class derived from this one, decorated with same attribute (with same key, different value)

[CustomDesigner(someKey, someOtherValue)]
public class ChildClass : BaseClass

Is there possibility that ChildClass don't create a duplicate of attribute but overwrite a value for existing key instead (overwrites whole parent attribute)? If not, what's the best pattern for getting default value from BaseClass if ChildClass has not defined his own?


回答1:


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).




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/10787082/overwrite-customattribute-on-derived-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!