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?
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).
use [AttributeUsage(Inherited=false)]
to prevent the attribute being inherited by the derived class.
I think it is possible in the following way:
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With