GetPropertyValue
Gets the value of a property in a object through relection
Source
public static T GetPropertyValue<T>(this object source, string property)
{
if (source == null)
throw new ArgumentNullException("source");
var sourceType = source.GetType();
var sourceProperties = sourceType.GetProperties();
var propertyValue = (from s in sourceProperties
where s.Name.Equals(property)
select s.GetValue(source, null)).FirstOrDefault();
return propertyValue != null ? (T)propertyValue : default(T);
}
Example
user.GetPropertyValue<string>("Name");