I have a binding to an unknown source. All I have is the binding. I have no other way of looking at the bound object. I need to figure out the Type for the bound object, even if the value is null (this is where my problem is).
I was evaluating the binding by binding to an object and then using the object as a way to get the Type, but I need to know the type even if the value is null.
For instance, I have a class like so:
public class Customer{
public string Name { get; set; }
public int Age { get; set; }
}
Now, if I have a WPF control bind to any of those properties (let’s assume they are dependency properties) I would like to get the type of the property, even if the value is null.
So, I have a custom control that now has a Binding object that represents {Binding Name} for instance. How can I get the type of the “bound object” using C#?
Are you willing to use reflection to get access to non-public members? If so, I think
Bindinghas an internal method calledCreateBindingExpressionthat returns aBindingExpression, which has a private member called_listenerof internal typePropertyPathListener. That has an internal property calledLeafType, which I believe is what you’re looking for.It’s messy, requires trust, and is subject to failure in future versions of the Framework, but it might be the only way to get what you’re looking for.