code

"모든 제네릭 유형"정의가있는 C # 제네릭 "where constraint"?

codestyles 2020. 8. 15. 09:16
반응형

"모든 제네릭 유형"정의가있는 C # 제네릭 "where constraint"?


예를 들어 보겠습니다.

  1. 일반적인 클래스 / 인터페이스 정의가 있습니다.

    interface IGenericCar< T > {...}

  2. 위의 클래스와 연결하려는 다른 클래스 / 인터페이스가 있습니다. 예를 들면 다음과 같습니다.

    interface IGarrage< TCar > : where TCar: IGenericCar< (**any type here**) > {...}

기본적으로 내 일반 IGarrage가 또는 IGenericCar여부에 관계없이 해당 유형에 대한 종속성이 없기 때문에 에 종속되기를 원합니다 .IGenericCar<int>IGenericCar<System.Color>


이를 달성하는 데 일반적으로 두 가지 방법이 있습니다.

Option1 : 제약 조건에 전달되어야 하는를IGarrage 나타내는 다른 매개 변수를 추가 합니다.TIGenericCar<T>

interface IGarrage<TCar,TOther> where TCar : IGenericCar<TOther> { ... }

Option2 : IGenericCar<T>일반적이지 않은 기본 인터페이스를 정의하고 해당 인터페이스에 대해 제한

interface IGenericCar { ... }
interface IGenericCar<T> : IGenericCar { ... }
interface IGarrage<TCar> where TCar : IGenericCar { ... }

다음과 같은 일을하는 것이 합리적일까요?

interface IGenericCar< T > {...}
interface IGarrage< TCar, TCarType > 
    where TCar: IGenericCar< TCarType > {...}

참고 URL : https://stackoverflow.com/questions/1541152/c-sharp-generic-where-constraint-with-any-generic-type-definition

반응형