How to get the property that has a DataMemberAttribute with a specified name?

XåpplI'-I0llwlg'I  - picture XåpplI'-I0llwlg'I - · Feb 3, 2013 · Viewed 8.8k times · Source

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.

Answer

// using System.Linq;
// using System.Reflection;
// using System.Runtime.Serialization;
obj.GetType()
   .GetProperties(…)
   .Where(p => Attribute.IsDefined(p, typeof(DataMemberAttribute)))
   .Single(p => ((DataMemberAttribute)Attribute.GetCustomAttribute(
                    p, typeof(DataMemberAttribute))).Name == "Foo");

Notes:

  • Attribute.IsDefined is used to check for the presence of a custom attribute without retrieving its data. Thus it is more efficient than Attribute.GetCustomAttribute and used to skip properties in a first step.

  • After the Where operator, we are left with properties that have exactly one DataMemberAttribute: Properties without this attribute have been filtered out, and it cannot be applied more than once. Therefore we can use Attribute.GetCustomAttribute instead of Attribute.GetCustomAttributes.