Search code examples
c#.netattributestypedescriptor

TypeDescriptor.AddAttributes() replaces the current attribute insead of adding it


I have this portion of code:

var hasAttribute = TypeDescriptor.GetAttributes(property.DeclaringType).OfType<CrmTypeAttribute>()
            .Any((attr)
                    => ((CrmTypeAttribute)attr).Name == property.Name);

            if (!hasAttribute)
            {
                var crmTypeAttribute = new CrmTypeAttribute(property.Name, crmType);
                TypeDescriptor.AddAttributes(property.DeclaringType, crmTypeAttribute);
            }

It has two problems:

  1. For some reason OfType returns an empty IEnumerable although it should return the right attributes of that type, and I checked. They exist.
  2. This is the severe problem. Instead of adding the attribute it replaces the old attribute of the same type with crmTypeAttribute. I have marked AllowMultiple to be true.

Can anyone tell me what's wrong with this code?
EDIT:
For some reason it allows adding only one attribute of an attribute type, I have added another attribute type at runtime and it worked.


Solution

  • Turns out that the attribute needs to override the TypeId property of the Attribute class to not be treated as duplicated.
    See here for details, it's very hidden and should be mentioned in GetAttributes as well.