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.
You could use LINQ:
or if you prefer:
I needed to get the value of the property and not the property itself so used Darin Dimitrov's answer but added
.GetValue(this)
to the end to return the value instead.Here is what my class ended up looking like:
Notes:
Attribute.IsDefined
is used to check for the presence of a custom attribute without retrieving its data. Thus it is more efficient thanAttribute.GetCustomAttribute
and used to skip properties in a first step.After the
Where
operator, 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.GetCustomAttribute
instead ofAttribute.GetCustomAttributes
.You could use Fasterflect to make your reflection code simpler and easier on the eyes:
If you only need to check for the presence of the attribute, something like this could be used instead:
Fasterflect comes with a nice set of extension methods and includes some neat performance optimizations using IL generation if you also need speed.