我无法从我的OrderedDictionary中找到值,我在堆栈上关注了几个帖子,所以我看不出我做得不好吗?
private List<IfcElement> readListParamFromList(Int32 index, OrderedDictionary ifcList)
{
List<IfcElement> retour = new List<IfcElement>();
if (this.ListParams[index] != "")
{
string[] listStrings = this.ListParams[index].Split(',');
foreach (string idString in listStrings)
{
long idElt = -1;
idElt = IfcGlobal.GetIdFromIfcString(idString);
try
{
object objectFound = (IfcElement)ifcList[(object)idElt];
IfcElement newElt = (IfcElement)objectFound;
retour.Add(newElt);
this.addChildren(newElt);
}
catch
{
this.ErrorFound = true;
this.ListItemsNotFound.Add(idElt);
}
}
}
return retour;
}在这里我找到了IdElt=104。然后我签入debug,我的OrderedDictionary中有一个带有Key=104的元素,并且object存在于value中,但是object objectFound = (IfcElement)ifcList[(object)idElt];行总是返回null。我的语法有问题吗?也许它有帮助,我添加元素的方式在我的字典中:
public class GroupedListElements
{
public string Type { get; set; } = "";
public OrderedDictionary ListIfcElements = new OrderedDictionary();
public GroupedListElements()
{
}
}
GroupedListElements newGroupList = new GroupedListElements { Type =
newElt.GetType().Name };
newGroupList.ListIfcElements.Add(newElt.ID, newElt);
this.ListGroupped.Add(newGroupList);发布于 2020-12-09 17:12:53
我认为问题在于,在从有序字典中检索对象之前,通过强制转换为( object ),字典试图根据Object.Equals()来定位键。如果装箱的long对象具有相同的引用,则Object.Equals返回true (即ReferenceEquals方法返回true)。如果您不介意使用字符串而不是长整型作为键,我建议您这样做。
要详细查看您的代码出了什么问题,可以替换以下代码行
object objectFound = (IfcElement)ifcList[(object)idElt];使用
object objectKey = (object)idElt;
object objectFound = (IfcElement)ifcList[objectKey];然后在调试器的“即时”窗口中查看objectKey.ReferenceEquals(x)是否对
ifcList.Keyshttps://stackoverflow.com/questions/65212708
复制相似问题