2017-11-17 2 views
0

을 반환하는 최우선 방법을 제한합니다.C#을 나뿐만 아니라 기본 클래스에서 상속하는 인터페이스를 구현해야하는 클래스의 인스턴스를 반환하는 방법을 가지고 있어야 추상 클래스가 특정 유형의

public abstract class AbstractClass 
{ 
    abstract [DonotKnowTheType] GetClassInstance() //The Return type should be an instance of a class which implements TestClass and ISample 
} 

public class ChildClass : AbstractClass 
{ 
    override [DonotKnowTheType] GetClassInstance() 
    { 
     //Need to return instance of SampleClass in this example. This could vary, this should be an instance of a class which implements TestClass and ISample 
    } 
} 

public class SampleClass : TestClass,ISample 
{ 
    //Implementation 
} 

좋은 디자인으로이를 달성하는 데 도움을주십시오. ChildClass에서 재정의 메서드를 작성하는 개발자가 TestClass 및 ISample을 구현하는 클래스의 인스턴스 만 반환하도록 제한해야합니다. 그렇지 않으면 컴파일 시간 오류가 발생합니다.

+0

어떤 방법이 있나요 이것을 성취하는 것? – Azeeb

+0

정직하게도 이것에 대한 이유가 없습니다, 나에게 이것은 나쁜 api 디자인입니다. –

+0

@FilipCordas 필자는 유창한 API 디자인이 그러한 특성을 통해 큰 이익을 얻을 수 있다고 생각합니다. –

답변

1

이 시도 :

public abstract class TheEnforcer<T> where T: TestClass, IMyInterface 
    { 
     protected abstract T GetClassInstance(); 
    } 

    public class ThePoorClass : TheEnforcer<DerivedTestClass> 
    { 
     protected override DerivedTestClass GetClassInstance() 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public class TestClass 
    { 

    } 

    public class DerivedTestClass : TestClass, IMyInterface 
    { 

    } 

    public interface IMyInterface 
    { 

    } 

를 귀하의 코멘트 후 :

namespace First { 
    public abstract class TheEnforcer<T> where T : IMarkerInterface 
    { 
     protected abstract T GetClassInstance(); 
    }  

    public interface IMarkerInterface 
    { 

    } } 

namespace Second { 
    using First; 

    // All this is in separate name space 
    public class TestClass: IMarkerInterface 
    { 

    } 

    public class DerivedTestClass : TestClass, IMyInterface 
    { 

    } 

    public interface IMyInterface 
    { 

    } 

    public class ThePoorClass : TheEnforcer<DerivedTestClass> 
    { 
     protected override DerivedTestClass GetClassInstance() 
     { 
      throw new NotImplementedException(); 
     } 
    } } 
+0

의 TestClass 레이어에 표시되지 않습니다 감사합니다. 마커 인터페이스는 비어있는 인터페이스 일뿐입니다. 희망이 해결됩니다. 솔직히, 이런 일을해야한다면. 나는 당신이 당면한 나쁜 OO 디자인을 가지고 있다고 생각한다. - 먼저 고쳐 져야한다. 이로써 – Azeeb

+0

있는 위치를 ...... –

1

당신은 TestClassISamplecontraint로 추상 클래스는 일반적인 만들 수 :

public abstract class AbstractClass<T> where T: TestClass, ISample 
{ 
    public abstract T GetClassInstance(); //The Return type should be an instance of a class which implements AbstractClass and ISample 
} 

public class ChildClass : AbstractClass<SampleClass> 
{ 
    public override SampleClass GetClassInstance() 
    { 
     //Need to return instance of SampleClass in this example. This could vary, this should be an instance of a class which implements AbstractClass and ISample 
     return new SampleClass(); 
    } 
} 
+0

, 그것은 ISample을 구현하기 위해 SampleClass을 적용하지만, 나는 그것이 baseTestClass 너무 .. – Azeeb

+0

@Azeeb에서 상속 있는지 확인해야합니다, TestClass에 오타라고 생각을 고정 : 편집 –

+0

@RandRandom 보는 것을 간과 –

관련 문제