我正在尝试编写一个方法,它可以获取viewModel中的所有ObservableCollections,并将它们转换为ObservableCollection<object>
。使用反射我已经能够获得每个ObservableCollection<T>
作为一个对象,但是我很难将这个对象转换为ObservableCollection<object>
。这里是我的代码到目前为止:将对象投射为ObservableCollection <object>
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>
}
}
有谁知道我该怎么做?
投射到'ObservableCollection