I'm trying to check if a property has the DataMemberAttribute applied (using TypeDescriptor)
this is what I have now:
PropertyDescriptor targetProp = targetProps[i];
var has = argetProp.Attributes.Contains(
Attribute.GetCustomAttribute(typeof(DataMemberAttribute).Assembly,typeof(DataMemberAttribute)));
the problem is that
Attribute.GetCustomAttribute(typeof(DataMemberAttribute).Assembly,typeof(DataMemberAttribute))
returns null
You could use LINQ. A chain of the .OfType<T>()
and .Any()
extension methods would do the job just fine:
PropertyDescriptor targetProp = targetProps[i];
bool hasDataMember = targetProp.Attributes.OfType<DataMemberAttribute>().Any();
There are 3 ways:
First:
PropertyDescriptor targetProp = targetProps[i];
bool hasDataMember = targetProp.Attributes.Contains(new DataMemberAttribute());
Second:
PropertyDescriptor targetProp = targetProps[i];
bool hasDataMember = targetProp.Attributes.OfType<DataMemberAttribute>().Any();
Third:
PropertyDescriptor targetProp = targetProps[i];
bool hasDataMember = targetProp.Attributes.Matches(new DataMemberAttribute());
Best Regard!
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