How can I make this code more generic
Posted
by Greg
on Stack Overflow
See other posts from Stack Overflow
or by Greg
Published on 2010-05-06T18:16:03Z
Indexed on
2010/05/06
18:28 UTC
Read the original article
Hit count: 201
Hi
How could I make this code more generic in the sense that the Dictionary key could be a different type, depending on what the user of the library wanted to implement? For example someone might what to use the extension methods/interfaces in a case where there "unique key" so to speak for Node is actually an "int" not a "string" for example.
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>();
}
}
© Stack Overflow or respective owner