我遇到的问题是,在少数几类课堂和交接点中,找不到理想的行为。
我的问题是:
//Inside a Unit Test that has access to internal methods and properties
INode firstNode, secondNode;
INodeId id = new NodeId (4);
first = new Node (id, "node");
second = new Node (id, "node");
Assert.IsTrue (first == second);
上文的说法之所以失败,是因为似乎将目标类别同起来,而不是在Node和NodeId班上超负荷运行。
如果你对我如何获得理想行为提出任何建议,那将是很麻烦的。
这里是我所致力于的框架的一部分:
public interface IIdentifier<T> where T : class
{
TKeyDataType GetKey<TKeyDataType> ();
bool Equals (IIdentifier<T> obj;
}
public interface INode
{
string name
{
get;
}
INodeId id
{
get;
}
}
public interface INodeId : IIdentifier<INode>
{
}
public class Node : INode
{
internal Node(INodeId id, string name)
{
//Work
}
public static bool operator == (Node n1, Node n2)
{
return n1.equals(n2);
}
public static bool operator != (Node n1, Node n2)
{
return !n1.equals(n2);
}
public bool Equals (INode node)
{
return this.name == node.name &&
this.id = node.id;
}
#region INode Properties
}
public class NodeId : INodeId
{
internal NodeId(int id)
{
//Work
}
public static bool operator == (NodeId n1, NodeId n2)
{
return n1.equals(n2);
}
public static bool operator != (NodeId n1, NodeId n2)
{
return !n1.equals(n2);
}
public override bool Equals (object obj)
{
return this.Equals ((IIdentifier<INode>) obj);
}
public bool Equals (IIdentifier<INode> obj)
{
return obj.GetKey<int>() == this.GetKey<int>();
}
public TKeyDataType GetKey<TKeyDataType> ()
{
return (TKeyDataType) Convert.ChangeType (
m_id,
typeof (TKeyDataType),
CultureInfo.InvariantCulture);
}
private int m_id;
}