How can I reflectively get the property that has the DataMember with a given name (let’s assume every DataMember has a unique name)? For example, in the following code the property with the DataMember that has name “p1” is PropertyOne:
[DataContract(Name = "MyContract")]
public class MyContract
{
[DataMember(Name = "p1")]
public string PropertyOne { get; set; }
[DataMember(Name = "p2")]
public string PropertyTwo { get; set; }
[DataMember(Name = "p3")]
public string PropertyThree { get; set; }
}
Currently, I have:
string dataMemberName = ...;
var dataMemberProperties = typeof(T).GetProperties().Where(p => p.GetCustomAttributes(typeof(DataMemberAttribute), false).Any());
var propInfo = dataMemberProperties.Where(p => ((DataMemberAttribute)p.GetCustomAttributes(typeof(DataMemberAttribute), false).First()).Name == dataMemberName).FirstOrDefault();
This works, but it feels like it could be improved. I particularly don’t like that GetCustomAttributes() is called twice.
How can it be re-written better? Ideally, it would be great if I could make it a simple one-liner.
Notes:
Attribute.IsDefinedis used to check for the presence of a custom attribute without retrieving its data. Thus it is more efficient thanAttribute.GetCustomAttributeand used to skip properties in a first step.After the
Whereoperator, we are left with properties that have exactly oneDataMemberAttribute: Properties without this attribute have been filtered out, and it cannot be applied more than once. Therefore we can useAttribute.GetCustomAttributeinstead ofAttribute.GetCustomAttributes.