How do I use a type with generic arguments as a constraint?


I would like to specify a constraint which is another type with a generic argument.

class KeyFrame<T>
    public float Time;
    public T Value;

// I want any kind of Keyframe to be accepted
class Timeline<T> where T : Keyframe<*>

But this cannot be done in c# as of yet, (and I really doubt it will ever be). Is there any elegant solution to this rather than having to specify the type of the keyframe argument?:

class Timeline<TKeyframe, TKeyframeValue>
     where TKeyframe : Keyframe<TKeyframeValue>,

Read about this from Eric Lippert's blog Basically, you have to find a way to refer to the type you want without specifying the secondary type parameter.

In his post, he shows this example as a possible solution:

public abstract class FooBase
  private FooBase() {} // Not inheritable by anyone else
  public class Foo<U> : FooBase {...generic stuff ...}

  ... nongeneric stuff ...

public class Bar<T> where T: FooBase { ... }
new Bar<FooBase.Foo<string>>()

Hope that helps, Troy