dmo
dmo

Reputation: 4083

Casting a generic collection to base type

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?

Upvotes: 6

Views: 9905

Answers (7)

eli
eli

Reputation: 177

ICollection<DerivedClass> cachedCollection;
Type cachedType;

public void PopulateList<BaseClass>(ICollection<T> collection) {
    cachedCollection = collection;
    cachedType = T;

    // other stuff...
}

private void Refresh() {
    PopulateList<cachedType>(cachedCollection.ToArray());
}

Calling cachedCollection.ToArray() worked for me.

Upvotes: 0

gdbdable
gdbdable

Reputation: 4501

You can cast each element into new iterator:

var collectionOfBase = (collectionOfDerived as IEnumerable).Cast<BaseClass>();

Upvotes: 0

Emperor XLII
Emperor XLII

Reputation: 13432

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>( ) );
}

Upvotes: 2

Jim Petkus
Jim Petkus

Reputation: 4508

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/

Upvotes: 6

Steve Py
Steve Py

Reputation:

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.

Upvotes: 2

Doug
Doug

Reputation: 1738

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.

Upvotes: 9

ChrisW
ChrisW

Reputation: 56113

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());
}

Upvotes: 4

Related Questions