views:

1474

answers:

5

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?

+4  A: 

The short answer is "No." You would need to create a new collection with the new BaseClass type and populate it.

If you think about it, it makes sense. If you could simply "cast" your collection to BaseClass, you could then stick something that was of type "BaseClass" into it, thereby forcing a simple BaseClass object into a DerivedClass Collection. This would defeat the purpose creating a Collection with Type-Safety.

In the extreme case, if you had two derived classes, Foo and Bar, you could force Bar objects into a collection of Foo objects by type-casting the collection back to a collection of BaseClass.

Doug
That makes sense. I'm only reading from the collection, so I wasn't thinking about the full implications of what I wanted to do.
dmo
+2  A: 

I don't think it's possible, and indeed it shouldn't be:

class BaseClass {}
class DerivedClass : BaseClass {}
class OtherClass : BaseClass {}

void test()
{
  List<DerivedClass> list = new List<DerivedClass>();
  slice(list); //you want to do this
}

void slice(IList<BaseClass> list)
{
  //yikes! adding OtherClass to List<DerivedClass>
  list.Add(new OtherClass());
}
ChrisW
In .NET 4.0 this situation is resolved through the use of the "in" and "out" keywords.
emddudley
+2  A: 

Switching between generic containers of derived classes and base classes is not supported. (It does work with arrays) It is possible to write a type converter to reasonably cleanly perform the switch without manually copying elements between the lists.

Check out this link for a description of the issue/limitation and a solution: http://www.25hoursaday.com/weblog/CommentView.aspx?guid=AF7AA888-A227-454C-8687-71FA77186064

Down at the bottom is a nice generic-enabled version.

Yep, ConvertAll is your friend here :)
Martin Clarke
+5  A: 

This is called co-variance and while not currently supported with generics in .net, it will be added in the 4.0 framework (along with the opposite which is contra-variance).

This excellent video from PDC 2008 is session on C# futures given by Anders Hejlsberg:

http://channel9.msdn.com/pdc2008/TL16/

Jim Petkus
Thanks for the info, Jim. I'll have a look at that.
dmo
+1  A: 

If .NET 3.5 is an option, you can use the Cast<T> extension method in System.Core.dll to get a read-only IEnumerable<T> for the collection:

using System.Linq;
...
private void Refresh() {
  PopulateList( cachedCollection.Cast<cachedType>( ) );
}
Emperor XLII