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