I am having problems getting the desired behavior out of these few classes and interfaces.
Here is my problem,
//Inside a Unit Test that has access to internal methods and properties
INode firstNode, secondNode;
INodeId id = new NodeId (4);
first = new Node (id,开发者_Go百科 "node");
second = new Node (id, "node");
Assert.IsTrue (first == second);
The assert above is failing because it seems to be going to the object class's equals method instead of the overloaded operator in the Node and NodeId classes.
If you have any suggestions on how I can get the desired behavior, that would be awesome.
Here is part of the Framework I am working on:
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;
}
Operator overloads are resolved at compile time based on the declared types of the operands, not on the actual type of the objects at runtime. An alternate way of saying this is that operator overloads aren't virtual. So the comparison that you're doing above is INode.operator==
, not Node.operator==
. Since INode.operator==
isn't defined, the overload resolves to Object.operator==
, which just does reference comparison.
There is no really good way around this. The most correct thing to do is to use Equals()
rather than ==
anywhere the operands might be objects. If you really, really need a fake virtual operator overload, you should define operator ==
in the root base class that your objects inherit from, and have that overload call Equals
. Note, however, that this won't work for interfaces, which is what you have.
I think you might need to override Equals(object) in Node like you did in NodeId. So:
public override bool Equals (object obj)
{
if (obj is Node)
{
return this.Equals(obj as Node);
}
return false;
}
// your code (modified to take a Node instead of an INode)
public bool Equals (Node node)
{
return this.name == node.name &&
this.id = node.id;
}
it's using the == from object because firstNode and secondNode aren't of type Node, they're of type INode. The compiler isn't recognizing the underlying types.
Since you can't overload an operator in an interface, your best bet is probably to rewrite the code so that it doesn't use the INode interface.
精彩评论