2014-04-08 1 views
1

나는 다음 C# 클래스와 인터페이스하는이 : 이제목록 항목이 클래스 및 인터페이스 모두를 준수합니까?

class NativeTool 
class NativeWidget: NativeTool 
class NativeGadget: NativeTool 
// above classes defined by the API I am using. Below classes and interfaces defined by me. 
interface ITool 
interface IWidget: ITool 
interface IGadget: ITool 
class MyTool: NativeTool, ITool 
class MyWidget: NativeWidget, IWidget 
class MyGadget: NativeGadget, IGadget 

을, 나는 아이들의 목록을 유지하는 MyTool을하고 싶습니다. 아이들은 모두 ITool을 준수하고 NativeTool에서 상속받습니다. MyTool, MyWidget 및 MyGadget 클래스는 모두이 기준에 부합합니다.

내 질문에 MyTool에 NativeTool과 ITool 모두에서 상속받을 것이라고 알리는 방법이 있습니까? 나는 쉽게 하나 또는 다른 것을 할 수 있습니다. 하지만 둘 다요?

+0

'NativeTool'과 'ITool'은 단순히 연결되어 있지 않습니다. 현재 원하는대로 지원되지 않습니다. 모든 제한 사항은 런타임에 점검해야하는데, 이는 사용자가 원하는 것이 아닐 수 있습니다 (해결해야 할 것이지만). – decPL

답변

0

처럼 유도체. 성가신 수의 래퍼이지만 저장소를 복제하지 않고도 작업이 완료됩니다.

public interface ITool { } 
public interface IWidget : ITool { } 
public class NativeTool { } 
public class NativeWidget : NativeTool { } 
public class MyTool : NativeTool, ITool, INativeTool { 
    public MyTool() { 
    this.Children = new List<INativeTool>(); 
    } 
    public ITool InterfacePayload { get { return this; } } 
    public NativeTool NativePayload { get { return this; } } 
    public List<INativeTool> Children { get; set; } 
    public NativeTool NativeChild(int index) { 
    return this.Children[index].NativePayload; 
    } 
    public ITool InterfaceChild(int index) { 
    return this.Children[index].InterfacePayload; 
    } 
    public void AddChild(MyTool child) { 
    this.Children.Add(child); 
    } 
    public void AddChild(MyWidget child) { 
    this.Children.Add(child); 
    } 
} 
public class MyWidget : NativeWidget, IWidget, INativeTool { 
    public ITool InterfacePayload { get { return this; } } 
    public NativeTool NativePayload { get { return this; } } 
} 
public interface INativeTool { 
    // the two payloads are expected to be the same object. However, the interface cannot enforce this. 
    NativeTool NativePayload { get; } 
    ITool InterfacePayload { get; } 
} 
public class ToolChild<TPayload>: INativeTool where TPayload : NativeTool, ITool, INativeTool { 
    public TPayload Payload { get; set; } 
    public NativeTool NativePayload { 
    get {return this.Payload;} 
    } 
    public ITool InterfacePayload { 
    get { return this.Payload; } 
    } 
} 
0

당신은 같은 것을 할 수 있습니다

public class MyTool<T,U> where T: ITool where U: NativeTool 
{ 
} 

와 같은를 만들 :

var tool = new MyTool<MyWidget, MyWidget>(); 

을 또한이 그것을 할 것으로 보인다

public class MyWidget : MyTool<....> 
    { 
    } 
관련 문제