C # generic " where constraint "with" any generic type " definition?
Permítanme dar un ejemplo:
-
Tengo una definición genérica de clase / interfaz:
interface IGenericCar< T > {...}
-
Tengo otra clase/interfaz que quiero relacionar con la clase anterior, por ejemplo:
interface IGarrage< TCar > : where TCar: IGenericCar< (**any type here**) > {...}
Básicamente, quiero que mi IGarrage genérico dependa de IGenericCar
, independientemente de si es IGenericCar<int>
o IGenericCar<System.Color>
, porque no tengo ninguna dependencia de ese tipo.
91
Author: Vegard Larsen, 2009-10-09
2 answers
Normalmente hay 2 maneras de lograr esto
Option1: Añadir otro parámetro a IGarrage represnting la T que se debe pasar a la restricción IGenericCar<T>
interface IGarrage<TCar,TOther> where TCar : IGenericCar<TOther> { ... }
Opción2: Defina una interfaz base para IGenericCar<T>
que no sea genérica y restrinja esa interfaz
interface IGenericCar { ... }
interface IGenericCar<T> : IGenericCar { ... }
interface IGarrage<TCar> where TCar : IGenericCar { ... }
120
Author: JaredPar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-08 23:50:52
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-08 23:50:52
¿Tendría sentido hacer algo como:
interface IGenericCar< T > {...}
interface IGarrage< TCar, TCarType >
where TCar: IGenericCar< TCarType > {...}
6
Author: snarf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-09 22:00:56
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-10-09 22:00:56