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:
- For some reason OfType returns an empty IEnumerable although it should return the right attributes of that type, and I checked. They exist.
- 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.