0
它作爲具體的MyCollection的
ObservableStateCollection
,對於簡單的目的,是這樣的:
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);
}
//...
}
注意該類型T
必須從StateObservable
導出。
現在,我深深地思考着。我會留下「爲什麼」的細節,並告訴你我目前在哪裏。我需要檢查我的型號上的特定屬性是否爲和ObservableStateCollection<T>
,並使用foreach循環訪問方法GetAll()
。
目前,我在:
if(prop.PropertyType.GetGenericTypeDefinition() == typeof(ObservableStateCollection<>))
{
var collection = (ObservableStateCollection<StateObservable>)prop.GetValue(model, null);
foreach (var e in collection.GetAll())
{
//act on ObservableStateCollection<StateObservable>
}
}
會拋出就行var collection = ...
個例外,因爲我不能投ObservableStateCollection<DerivedType>
到ObservableStateCollection<BaseType>
什麼是我選擇這裏?如何獲得一個強類型對象,我可以撥打GetAll
?