code_junkie

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

Nov 11th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. class KeyFrame<T>
  2. {
  3. public float Time;
  4. public T Value;
  5. }
  6.  
  7. // I want any kind of Keyframe to be accepted
  8. class Timeline<T> where T : Keyframe<*>
  9. {
  10. }
  11.  
  12. class Timeline<TKeyframe, TKeyframeValue>
  13. where TKeyframe : Keyframe<TKeyframeValue>,
  14. {
  15. }
  16.  
  17. public abstract class FooBase
  18. {
  19. private FooBase() {} // Not inheritable by anyone else
  20. public class Foo<U> : FooBase {...generic stuff ...}
  21.  
  22. ... nongeneric stuff ...
  23. }
  24.  
  25. public class Bar<T> where T: FooBase { ... }
  26. ...
  27. new Bar<FooBase.Foo<string>>()
  28.  
  29. class TimeLine<T>
  30. {
  31. private IList<KeyFrame<T>> keyFrameList;
  32. ...
  33. }
  34.  
  35. class Timeline<T>
  36. {
  37. List<KeyFrame<T>> _frames = new List<KeyFrame<T>>(); //Or whatever...
  38.  
  39. ...
  40. }
  41.  
  42. class KeyFrame<T> {
  43. public float Time;
  44. public T Value;
  45.  
  46. class Timeline<U> where U : Keyframe<T> {
  47. }
  48. }
Add Comment
Please, Sign In to add comment