I have this piece 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 correct attributes of this type, and I checked. They exist.
- This is a serious problem. Instead of adding an attribute, it replaces an old attribute of the same type with crmTypeAttribute. I set AllowMultiple to true.
Can someone tell me what is wrong with this code? EDIT:
For some reason, it allows you to add only one attribute type attribute, I added another attribute type at runtime and worked.
source share