2

我有一个简单的项目,使用 MVC 模式、ET 和 WCF。它使用 Dijkstra 来寻找城市之间的路径。这就是类本身。

但是,当调用对象FindPathFromCityToCity()中的两个相同列表时map,相同fromto,当它在普通控制台应用程序上时,它会返回所需的结果。但是,当从 WCF 中使用时,它会在第 39 行失败并出现异常System.Collections.Generic.KeyNotFoundException

为了检查我的输入是否不正确,我添加了写入 txt 文件(第 17 到 27 行),这就是结果(TL;DR - 两者完全相同):

安慰:

COUNT: 14 - RANGE 120
FROM: Aalborg - TO: Copenhagen
C: Frederikshavn - E: 2 - T: BLL.BatteryCenter
C: Aalborg - E: 6 - T: BLL.BatteryCenter
C: Hobro - E: 4 - T: BLL.BatteryCenter
C: Randers - E: 6 - T: BLL.BatteryCenter
C: Viborg - E: 8 - T: BLL.BatteryCenter
C: Aarhus - E: 8 - T: BLL.BatteryCenter
C: Herning - E: 6 - T: BLL.BatteryCenter
C: Vejle - E: 8 - T: BLL.BatteryCenter
C: Kolding - E: 6 - T: BLL.BatteryCenter
C: Odense - E: 6 - T: BLL.BatteryCenter
C: Aabenraa - E: 2 - T: BLL.BatteryCenter
C: Koge - E: 4 - T: BLL.BatteryCenter
C: Copenhagen - E: 2 - T: BLL.BatteryCenter
C: Soro - E: 4 - T: BLL.BatteryCenter

周转基金:

COUNT: 14 - RANGE 120
FROM: Aalborg - TO: Copenhagen
C: Frederikshavn - E: 2 - T: BLL.BatteryCenter
C: Aalborg - E: 6 - T: BLL.BatteryCenter
C: Hobro - E: 4 - T: BLL.BatteryCenter
C: Randers - E: 6 - T: BLL.BatteryCenter
C: Viborg - E: 8 - T: BLL.BatteryCenter
C: Aarhus - E: 8 - T: BLL.BatteryCenter
C: Herning - E: 6 - T: BLL.BatteryCenter
C: Vejle - E: 8 - T: BLL.BatteryCenter
C: Kolding - E: 6 - T: BLL.BatteryCenter
C: Odense - E: 6 - T: BLL.BatteryCenter
C: Aabenraa - E: 2 - T: BLL.BatteryCenter
C: Koge - E: 4 - T: BLL.BatteryCenter
C: Copenhagen - E: 2 - T: BLL.BatteryCenter
C: Soro - E: 4 - T: BLL.BatteryCenter

有人能看出发生这种情况的任何原因吗?


编辑:

我做了进一步的测试,用这个片段打印了所有可能的对象,并且对象没有区别(当然哈希除外):

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\log\check.txt", true))
{
    foreach (var m in map.Stations)
    {
        file.WriteLine(" --- {0} - {1} - {2} - {3} --- ", m.name, m.Edgelist.Count, m.GetType(), m.GetHashCode());
        foreach(var e in m.Edgelist)
        {
            file.WriteLine();
            file.WriteLine(" # {0} - {1} - {2} - {3}", e.BatteryStation.name, e.BatteryStation1.name, e.distance, e.edgeID);
            file.WriteLine(" + {0} - {1} - {2}", e.BatteryStation.GetType(), e.BatteryStation.stationID, e.BatteryStation.GetHashCode());
            file.WriteLine(" - {0} - {1} - {2}", e.BatteryStation1.GetType(), e.BatteryStation1.stationID, e.BatteryStation1.GetHashCode());
        }
        file.WriteLine();
   }
}
4

1 回答 1

1

这一切都取决于BatteryCenter物体的力学。

调用代码将其作为参数传入,函数将其用作映射中的键。

您是否覆盖了函数EqualsGetHashCodeon BatteryCenter

如果不是,那么字典查找将搜索那个确切的对象。不是BatteryCenter具有所有相同字段的 a,而是具有相同引用的对象。

WCF 客户端将传入一个等效但仍然不同BatteryCenter的 ,字典无法找到它。

一旦你添加了覆盖BatteryCenter.EqualsBatteryCenter.GetHashCode这可能会起作用。

例如

public override int GetHashCode() 
{
     return this.stationID.GetHashCode() ^ this.name.GetHashCode();
}

public override bool Equals(Object obj) 
{
     if(Object.ReferenceEquals(this, obj))
     {
          return true;
     }
     BatteryStation other = obj as BatteryStation;
     if(Object.ReferenceEquals(other, null))
     {
         return false;
     }
     return ((this.stationID == other.stationID) && (this.name == other.name));
}
于 2013-12-19T01:57:42.243 回答