Here's an extension method that evaluates a property's value and also returns the corresponding DependencyProperty:
public static bool TryEvaluateProperty(this FrameworkElement fe, string property, out object value, out DependencyProperty dp)
{
value = default;
dp = default;
var feType = fe.GetType();
var propertyInfo = feType.GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
if (propertyInfo == null) return false;
value = propertyInfo.GetValue(fe);
var dpName = property + "Property"; //ASSUMPTION: Definition uses DependencyProperty naming convention
while (feType != null)
{
var dpField = feType.GetField(dpName, BindingFlags.Static | BindingFlags.Public);
if (dpField != null)
{
dp = dpField.GetValue(null) as DependencyProperty; //use obj==null for static field
break;
}
feType = feType.BaseType;
}
return true;
}