Jake
Jake

Reputation: 11430

C# type casting to a generic interface

I am writing a library that renders a bunch of child objects to screen. The child object is abstract, and it is intended for users of this library to derive their own child from this abstract class.

public abstract class Child : IRenderable {}

public interface IParent<T> where T : Child
{
   IEnumerable<T> Children { get; }
}

The complication is that I do not have a list of IParent to work with, instead, I have a bunch of IRenderables. The user of the library is expected to write something like this:

public class Car : IRenderable { }
public class Cow : IRenderable, IParent<Calf> { }
public class Calf : Child { }

// note this is just an example to get the idea
public static class App
{
   public static void main()
   {
      MyLibraryNameSpace.App app = new MyLibraryNameSpace.App();
      app.AddRenderable(new Car()); // app holds a list of IRenderables
      app.AddRenderable(new Cow());
      app.Draw(); // app draws the IRenderables
   }
}

In Draw(), the library should cast and check whether the IRenderable is also an IParent. However, since I do not know about the Calf, I don't know what to cast Cow into.

// In Draw()
foreach(var renderable in Renderables)
{
   if((parent = renderable as IParent<???>) != null) // what to do?
   {
      foreach(var child in parent.Children)
      {
          // do something to child here.
      }
   }
}

How can I overcome this problem? Is this anything to do with covariance generics or what-so-ever (I am not familiar with the covariance concept)?

Upvotes: 7

Views: 6279

Answers (3)

Heinzi
Heinzi

Reputation: 172220

Since IParent<T> only returns items of type T, you could make it covariant using the out modifier:

public interface IParent<out T> where T : Child
{
   IEnumerable<T> Children { get; }
}

This would make IParent<anything> convertible to IParent<Child>:

IParent<Child> parent = renderable as IParent<Child>; // works for Cow

Note that covariance only works as long as you are only returning objects of type T (simply speaking). For example, as soon as you add an AddChild(T) method to your IParent interface, covariance must break (= the compiler will complain), since, otherwise, the following type-unsafe code could be written:

IParent<Child> parent = renderable as IParent<Child>;
parent.AddChild(new Kitten()); // can't work if parent is really a Cow.

Upvotes: 9

Brad Mccormack
Brad Mccormack

Reputation: 84

Something along the following lines?

static void draw(List<IRenderable> renderables)
{
    foreach (IRenderable render in renderables)
    {
        if (render is IParent<Child>)
        {
            foreach (Child c in ((IParent<Child>)render).Children)
            {
                //do something with C?
            }
        } 
    }
}

Upvotes: 1

Dmitry Reznik
Dmitry Reznik

Reputation: 6862

You could implement intermediate non-generic interface IParent:

public interface IParent
{
    IEnumerable<Child> Children { get; }
}

public interface IParent<T> : IParent
  where T: Child
{
    IEnumerable<T> Children { get; }
}

And then cast to IParent in your function.

Upvotes: 1

Related Questions