【发布时间】:2020-07-12 12:57:08
【问题描述】:
当我运行代码时,它返回 -1,尽管对象(在我的理解中)必须存在,因为它是由 foreach 语句生成的。 有人有想法吗?
public partial class Class1
{
private List<Master_Menu_Item> Config
{
get { return JsonConvert.DeserializeObject<List<Master_Menu_Item>>(Json_string.Config); } //if you need an example json, i can provide it
}
private void Reload_Master_stack()
{
if (Config != null)
{
foreach (Master_Menu_Item master_Menu_Item in Config)
{
int index = Config.IndexOf(master_Menu_Item);
Debug.WriteLine(index);
}
}
}
}
class Master_Menu_Item
{
public string Name { get; set; }
public string Icon_path { get; set; }
public List<Bottom_Menu_Item> Bottom_Menu_Items { get; set; }
}
【问题讨论】:
-
我的猜测是
Master_Menu_Item会覆盖Equals,但做得很糟糕。我们不能仅仅从这段代码中分辨出来。请提供minimal reproducible example - 它应该是完整的,但不能包含任何不相关的内容。 -
告诉我们
Config是如何声明的。 -
请阅读minimal reproducible example,以便您了解minimal reproducible example 的含义。编辑表示赞赏,但未能提供理解您的问题所需的minimal reproducible example。