我遇到了从这几个类和接口中获得所需行为的问题. 这是我的问题, //Inside a Unit Test that has access to internal methods and propertiesINode firstNode, secondNode;INodeId id = new NodeId (4);first = new Node (id, "no
这是我的问题,
//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);
上面的断言失败了,因为它似乎是在对象类的equals方法而不是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;
}
运算符重载在编译时根据声明的操作数类型解析,而不是在运行时的实际对象类型上解析.另一种说法是运算符重载不是虚拟的.所以你上面做的比较是INode.operator ==,而不是Node.operator ==.由于未定义INode.operator ==,因此重载将解析为Object.operator ==,它只是进行引用比较.
这没有什么好办法.最正确的做法是在操作数可能是对象的任何地方使用Equals()而不是==.如果你真的需要虚假的虚拟运算符重载,你应该在你的对象继承的根基类中定义operator ==,并让该重载调用Equals.但请注意,这不适用于接口,这就是您所拥有的.
