Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite customattribute on derived class

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?

like image 325
sasjaq Avatar asked May 28 '12 15:05

sasjaq


3 Answers

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

like image 54
Adriano Repetti Avatar answered Nov 05 '22 05:11

Adriano Repetti


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

like image 38
tech-man Avatar answered Nov 05 '22 06:11

tech-man


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.

like image 1
Creepin Avatar answered Nov 05 '22 06:11

Creepin