I am trying to write a method that gets all of the ObservableCollections in a viewModel and casts each as an ObservableCollection<object>
. Using reflection I've been able to get each ObservableCollection<T>
as an object, but I'm having difficulties casting this object to an ObservableCollection<object>
. Here is my code thus far:
var props = viewModel.GetType().GetProperties();
Type t = viewModel.GetType();
foreach (var prop in props)
{
if (prop.PropertyType.Name == "ObservableCollection`1")
{
Type type = prop.PropertyType;
var property = (t.GetProperty(prop.Name)).GetValue(viewModel);
// cast property as an ObservableCollection<object>
}
}
Does anyone know how I should proceed?