I have a class Human and need to get “IsChecked” value. ItemsSource may have different types, but all classes has IsChecked property.
public class Human{
public bool IsChecked{
get;
set;
}
}
private void CheckBoxLoaded(object sender, RoutedEventArgs e) //sender is CheckBox
//How set value from IsChecked?
var prop = t.GetType().GetProperties().Single(n => n.Name == "IsChecked");//I can access it by this method, but can't set value of sender which CheckBox;
}
I’m assuming that for some reason you can’t refactor your
Humanclass to implement someICheckableinterface, containingIsChecked, right?If you’re using C# 4.0, an alternative is to use the
dynamicdata type, which will allow you to set a property only checked at runtime:And if all else fails and you really have to use reflection, you’re 90% of the way there. You just need to call the
PropertyInfoobject’sGetValuemethod, giving it the instance you wish to retrieve the value from: