2013-04-20 1 views
0

내 코드에 CRTP 인터페이스를 구현하려고 시도했지만 제약으로 인해 문제가 발생합니다. 제약 조건을 구현하는 방법이 코드 구조가있는 경우? 이게 합법적인가? 고맙습니다.C#의 여러 호기심 반복 템플릿 패턴 (CRTP)?

interface IInterface<T> 
    where T: IInterface<T> 
{ 
    //bla bla bla 
    T Member { get; set; } 
} 
interface ITest1<iTest2, iTest1> : IInterface<iTest2> 
{ 
    //bla bla bla 
} 
interface ITest2<iTest1, iTest3> : IInterface<iTest1> 
{ 
    iTest3 RefMember { get; set; } 
    //bla bla bla 
} 
interface ITest3<iTest2> 
{ 
    List<iTest2> manyTest { get; set; } 
    //bla bla bla 
} 
class Test1 : ITest1<Test2, Test1> 
{ 
    //bla bla bla 
} 
class Test2 : ITest2<Test1, Test3> 
{ 
    //bla bla bla 
} 
class Test3 : ITest3<Test2> 
{ 
    //bla bla bla  
} 
+0

당신이 당신의 코드를 컴파일하는 것을 시도했다 기본 클래스

public abstract class CrtpBaseWrapper<T> : MyBase where T : CrtpBaseWrapper<T> { } 

은 당신이 당신의 서브 클래스 만들 수 있도록? 컴파일되지 않으면 어떤 오류가 발생합니까? – svick

+0

코드가 모두 잘못되었습니다. 컴파일러가 그것을 허용하더라도 ('interface IInterface T : IInterface '등). 나는 네가 원하는 것을 구조를 설명하는 것이 가장 좋다고 생각한다. – NSGaga

+0

@svick 그런 식으로 작성된 코드라면 오류가 없습니다. 그러나 제약 조건을 추가하려고하면 두통이 날 것입니다. 'ITest1 : 엔터티 여기서 iTest2 : ITest2 , ITest3 , ..... (?????)' – user2277061

답변

1
public abstract class MyBase 
{ 
    /// <summary> 
    /// The my test method. divyang 
    /// </summary> 
    public virtual void MyVirtualMethodWhichIsOverridedInChild() 
    { 
     Console.Write("Method1 Call from MyBase"); 
    } 

    /// <summary> 
    /// The my another test method. 
    /// </summary> 
    public abstract void MyAnotherTestMethod(); 

    /// <summary> 
    /// The my best method. 
    /// </summary> 
    public virtual void MyVirualMethodButNotOverridedInChild() 
    { 
     Console.Write("Method2 Call from MyBase"); 
    } 
} 

지금

public class CrtpChild : CrtpBaseWrapper<CrtpChild> 
{ 
    /// <summary> 
    /// The my test method. divyang 
    /// </summary> 
    public override void MyVirtualMethodWhichIsOverridedInChild() 
    { 
     Console.Write("Method1 Call from CrtpChild"); 
    } 

    /// <summary> 
    /// The my another test method. 
    /// </summary> 
    public override void MyAnotherTestMethod() 
    { 
     Console.Write("MyAnotherTestMethod Call from CrtpChild"); 
    } 
} 
+0

마침내 나는 당신의 방법을 사용했습니다. 고맙습니다 – user2277061