2013-02-17 11 views
0

У меня есть кое-что вдоль линий следующее:Определение общего ограничение, которое зависит от другого обобщенного класса

// This part is fine 
abstract class Attack {} 

abstract class Unit<A> where A : Attack {} 

class InstantAttack : Attack {} 
class Infantry : Unit<InstantAttack> {} 

// I'm lost here, on this part's syntax: 
abstract class Controller<U> where U : Unit {} 
// I want the above class to take any kind of Unit, but Unit is a generic class! 

выше не работает для Controller, потому что where U : Unit не прав, так как Unit требует родовое параметр (например, Unit<InstantAttack>). Я пробовал:

abstract class Controller<U<A>> where U<A> : Unit<A> {} 

который конечно не работал. Какой правильный синтаксис для этого?

ответ

2

Либо так:

abstract class Controller<U, A> where U : Unit<A> {} 

Или так:

interface IUnit { } 
abstract class Unit<A> : IUnit where A : Attack { } 
abstract class Controller<U> where U : IUnit { } 
+0

/Facepalm. Благодаря! – Cornstalks