2014-06-16 6 views
1

여기 내가하려는 일이 있습니다.제네릭 클래스에서 부모 클래스의 메서드에 액세스하는 방법?

public interface IHelper:IHtmlString 
{ 
    IHelper AddClass(string className); 
    IHelper Attributes(object htmlAttributes); 
} 

public class Helper : IHelper 
{ 
    private readonly Alert _parent; 

    public Helper(Alert parent) 
    { 
     _parent = parent; 
    } 

    public string ToHtmlString() 
    { 
     return ToString(); 
    } 

    public IHelper AddClass(string className) 
    { 
     return _parent.AddClass(className); 
    } 

    public IHelper Attributes(object htmlAttributes) 
    { 
     return _parent.Attributes(htmlAttributes); 
    } 
    public override string ToString() 
    { 
     return _parent.ToString(); 
    } 
} 

Alert 클래스 :

public interface IAlert : IHelper 
{ 
    IHelper HideCloseButton(bool hideCloseButton); 
    IHelper Success(); 
    IHelper Warning(); 
} 

public class Alert : IAlert 
{ 
    private readonly string _text; 
    private AlertStyle _style; 
    private bool _hideCloseButton; 
    private ICollection<string> _cssClass; 
    private object _htmlAttributes; 

    public Alert(string text, AlertStyle style, bool hideCloseButton = false, object htmlAttributes = null) 
    { 
     _text = text; 
     _style = style; 
     _hideCloseButton = hideCloseButton; 
     _htmlAttributes = htmlAttributes; 
    } 
    public override string ToString() 
    { 
     return ""; 
    } 

    public string ToHtmlString() 
    { 
     return RenderAlert(); 
    } 
    // private method RenderAlert() is omitted here. 

    public IHelper AddClass(string className) 
    { 
     if (_cssClass == null) _cssClass = new List<string>(); 
     _cssClass.Add(className); 
     return new Helper(this); 
    } 

    public IHelper Attributes(object htmlAttributes) 
    { 
     _htmlAttributes = htmlAttributes; 
     return new Helper(this); 
    } 

    public IHelper HideCloseButton(bool hideCloseButton) 
    { 
     _hideCloseButton = hideCloseButton; 
     return new Helper(this); 
    } 

    public IHelper Success() 
    { 
     _style = AlertStyle.Success; 
     return new Helper(this); 
    } 

    public IHelper Warning() 
    { 
     _style = AlertStyle.Warning; 
     return new Helper(this); 
    } 
} 

문제는 내 Helper 클래스의 생성자가 직접 Alert에 액세스

유창함 API와 정의 HTML 도우미를 만들려면,이 만든 . 그런 다음 내 IHelperHelper을 일반 IHelper<T>Helper<T>으로 변경하여 DropDownListCheckBoxGroup과 같은 다른 맞춤식 도우미에 사용하기가 어렵습니다.

AddClass 및는 다른 모든 HTML 도우미에게 제공되어야하므로 필자는 중복 코드를 원하지 않습니다. 그러나이 일반 클래스를 작성하는 올바른 방법은 무엇입니까?

+1

이 코드를 트리밍하여 질문을 이해하기 쉽습니다. –

+0

동의 함. 일부 코드를 삭제하겠습니다. – Blaise

+0

필요한 메소드를 자신의 타입/인터페이스에 넣고 제네릭 타입을 새로운 타입으로 제한 할 수 있어야합니다. – Jason

답변

1

AddClassAttributes을 포함하는 인터페이스를 만들고 Alert 클래스에 적용한 다음 유형 제약 조건 (IHelper<T> where T : IYourInterface)으로 추가하십시오.

+0

은 좋은 방향으로 들립니다. 뭐라고 해요? 'where T' 절이나 예제 페이지를 사용하기 위해 일부 문서에 대한 링크를 사용할 수 있습니까? – Blaise

+0

[형식 매개 변수에 대한 제약 조건 (C# 프로그래밍 가이드)] (http://msdn.microsoft.com/en-us/library/d5x73970.aspx) – CodeCaster

+1

고맙습니다. 그게 내가 필요한거야. 정말 감사합니다. – Blaise

관련 문제