I have a custom collection type ObservableStateCollection
that, for simplistic purposes, looks like:
public class ObservableStateCollection<T> : IList<T>, INotifyCollectionChanged, INotifyPropertyChanged where T : StateObservable
{
private List<T> _items;
private List<T> _deleted;
public IEnumerator<T> GetEnumerator()
{
return _items.GetEnumerator();
}
public IEnumerable<StateObservable> GetAll()
{
return _items.Concat(_deleted);
}
//...
}
Note that type T
must be derived from StateObservable
.
Now, I'm neck deep in reflection. I'll spare the details of 'why', and just show you where I'm currently at. I need to check if a particular property on my model is an ObservableStateCollection<T>
and use a foreach to loop through the GetAll()
method.
Currently, I'm at:
if(prop.PropertyType.GetGenericTypeDefinition() == typeof(ObservableStateCollection<>))
{
var collection = (ObservableStateCollection<StateObservable>)prop.GetValue(model, null);
foreach (var e in collection.GetAll())
{
//act on ObservableStateCollection<StateObservable>
}
}
which throws an exception on the line var collection = ...
, as I can't cast ObservableStateCollection<DerivedType>
to ObservableStateCollection<BaseType>
What are my options here? How to I get a strongly typed object back that I can call GetAll
on?
Aucun commentaire:
Enregistrer un commentaire