2010-05-06 4 views
1

라이브러리의 사용자가 구현하고자하는 내용에 따라 사전 키가 다른 유형이 될 수 있다는 점에서이 코드를보다 일반적으로 만들 수 있습니까? 예를 들어 누군가가 "고유 키"가있는 경우 확장 메소드/인터페이스를 사용할 수 있습니다. Node는 실제로 "int"이며 예를 들어 "문자열"입니다.이 코드를 좀 더 일반적인 것으로 만들 수있는 방법

public interface ITopology 
{ 
    Dictionary<string, INode> Nodes { get; set; } 
} 

public static class TopologyExtns 
{ 
    public static void AddNode(this ITopology topIf, INode node) 
    { 
     topIf.Nodes.Add(node.Name, node); 
    } 
    public static INode FindNode(this ITopology topIf, string searchStr) 
    { 
     return topIf.Nodes[searchStr]; 
    } 
} 

public class TopologyImp : ITopology 
{ 
    public Dictionary<string, INode> Nodes { get; set; } 

    public TopologyImp() 
    { 
     Nodes = new Dictionary<string, INode>(); 
    } 
} 

답변

6

인터페이스를 일반화 한 다음 Func<INode,T>을 키의 선택기로 사용하십시오. 이것은 사전에서 키를 노드에서 추출하기를 원한다고 가정합니다. 이것이 어려운 요구 사항이 아니라면 서명에 제네릭 형식 지정자를 사용하여 키 자체를 지정할 수 있습니다.

public interface ITopology<T> 
{ 
    Dictionary<T, INode> Nodes { get; set; } 
} 

public static class TopologyExtns 
{ 
    public static void AddNode<T>(this ITopology<T> topIf, INode node, Func<INode,T> keySelector) 
    { 
     topIf.Nodes.Add(keySelector(node), node); 
    } 
    public static INode FindNode<T>(this ITopology<T> topIf, T searchKey) 
    { 
     return topIf.Nodes[searchKey]; 
    } 
} 

public class TopologyImp<T> : ITopology<T> 
{ 
    public Dictionary<T, INode> Nodes { get; set; } 

    public TopologyImp() 
    { 
     Nodes = new Dictionary<T, INode>(); 
    } 
} 

또한 INode를 제네릭 유형으로 만드는 것이 좋습니다. 이렇게하면 구현을 적절한 "실제"키로 연기 할 수있는 제네릭 형식의 속성으로 Key를 지정할 수 있습니다. 이렇게하면 확장 메서드에 키 또는 선택기를 제공하지 않아도됩니다.

대안 :

public interface INode<T> 
{ 
    T Key { get; } 
    string Name { get; set; } 
    int ID { get; set; } 
} 

public class StringNode : INode<string> 
{ 
    public string Key { get { return this.Name; } } 
    public string Name { get; set; } 
    public int ID { get; set; } 
} 

public interface ITopology<T> 
{ 
    Dictionary<T, INode<T>> Nodes { get; set; } 
} 

public static class TopologyExtns 
{ 
    public static void AddNode<T>(this ITopology<T> topIf, INode<T> node) 
    { 
     topIf.Nodes.Add(node.Key, node); 
    } 
    public static INode<T> FindNode<T>(this ITopology<T> topIf, T searchKey) 
    { 
     return topIf.Nodes[searchKey]; 
    } 
} 

public class TopologyImp<T> : ITopology<T> 
{ 
    public Dictionary<T, INode<T>> Nodes { get; set; } 

    public TopologyImp() 
    { 
     Nodes = new Dictionary<T, INode<T>>(); 
    } 
} 

로 사용 : - ITopology 지금 일반, 당신은 확장 방법 일반 또는 매개 변수 참조 특정하기 위해 하나 필요

var topology = new TopologyImp<string>(); 
topology.AddNode(new StringNode { Name = "A", ID = 0 } ); 
var node = topology.FindNode("A"); 
2
public interface ITopology<T> 
{ 
    Dictionary<T, INode> Nodes { get; set; } 
} 

public static class TopologyExtns<T> 
{ 
    public static void AddNode<T>(this ITopology<T> topIf, T key, INode node) 
    { 
     topIf.Nodes.Add(key, node); 
    } 
    public static INode FindNode<T>(this ITopology<T> topIf, T searchKey) 
    { 
     return topIf.Nodes[searchKey]; 
    } 
} 

public class TopologyImp<T> : ITopology<T> 
{ 
    public Dictionary<T, INode> Nodes { get; set; } 

    public TopologyImp() 
    { 
     Nodes = new Dictionary<T, INode>(); 
    } 
} 
+0

TopologyExtns은 잘못된 것입니다. –

+0

@Matt에게 감사합니다. – derek

2

왜 당신의 토폴로지 인터페이스는 일반적인 만들어? 확장 메서드에 약간 익숙하지 않지만이 방법을 사용할 수 있어야합니다.

public interface ITopology<TKey> 
{ 
    Dictionary<TKey, INode> Nodes { get; set; } 
} 

public static class TopologyExtns 
{ 
    public static void AddNode<T>(this ITopology<T> topIf, T key, INode node) 
    { 
     topIf.Nodes.Add(key, node); 
    } 
    public static INode FindNode<T>(this ITopology<T> topIf, T searchObj) 
    { 
     return topIf.Nodes[searchObj]; 
    } 
} 


public class TopologyImp : ITopology<String> 
{ 

public TopologyImp() 
{ 
    Nodes = new Dictionary<String, INode>(); 
} 
} 
관련 문제