I've got an IList<DerivedClass>
that I want to cast to ICollection<BaseClass>
but when I attempt an explicit cast, I get null
. Is it possible to do this without creating and populating a new collection?
Edit:
Since I only want to read from the collection, I switched to using a generic method:
public void PopulateList<BaseClass>(ICollection<T> collection)
Then I can pass it an IList<DerivedClass>
. Is there a good way to cache this list so I can refresh it when I need to. My first inclination is to use:
Object cachedCollection;
Type cachedType;
public void PopulateList<BaseClass>(ICollection<T> collection) {
cachedCollection = collection;
cachedType = T;
// other stuff...
}
private void Refresh() {
PopulateList<cachedType>(cachedCollection as ICollection<cachedType>);
}
Does anyone have a better way of doing this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…