在我的代碼中,我有一個類,它維護着許多列表。我們現在將關注其中的一個,因爲這是突出顯示問題的一個。對象來自列表<T>似乎是副本,而不是參考
internal List<Badge> Badges { get; private set; }
在代碼中,當解析XML文檔時,我將Badge
實例添加到此列表中。稍後,我想更新列表中的各個實例,以便將數據寫回XML。由於數據的XML結構與原始文件結構有所不同,因此涉及到一些重點問題,但這很大程度上已經被繪製出來。當我嘗試更新List<Badge>
中的一個項目時,出現了意外。
具體來說,有問題的代碼是在這裏:
// Get the current badge from the loaded XML data, so we can update it.
var currentBadge = this.GameData.GetCurrentBadge();
我總是得到一個有效的徽章回來。令人驚訝的,因爲我是來看看,就是這個簡單的測試總是失敗:
var result = this.GameData.Badges.IndexOf(currentBadge);
result
始終計算爲-1,表明該對象不集合中存在。 (編輯:更新currentBadge
上的屬性對this.GameData.Badges
中的匹配項目的內容沒有任何影響。)這導致我得出結論我得到我的對象的副本,而不是一個參考,因爲我會預期。
對於好奇,從GameData類檢索徽章的代碼包含在下面。我懷疑這是通用列表的記錄行爲,而且這是我第一次偶然發現它。如果是這樣,我進入一個非常粗魯的覺醒。如果不是這樣,我真的很想知道爲什麼我的物體會從原來的「斷開」回來。
private Badge GetCurrentBadge()
{
var badgeItem = GetCurrentBadgeItem();
if (badgeItem != null)
{
return this.GameData.GetBadgeByText(badgeItem.Text);
}
return null;
}
private MenuOption GetCurrentBadgeItem()
{
if (!(this.currentItem is MenuOption &&
(this.currentItem as MenuOption).IsLocked))
{
return null;
}
MenuOption result = null;
var children = this.currentMenu.Children;
for (var n = children.Count - 1; n >= 0; n--)
{
var child = children[n] as MenuOption;
if (child == null || !child.IsLocked)
{
break;
}
if (!child.Text.StartsWith(" "))
{
result = child;
break;
}
}
return result;
}
更新:每個請求,GetBadgeByText,它來自GameData類。正如你所看到的,我已經嘗試過使用Linq和不使用Linq,只是爲了消除這個罪魁禍首。改變執行方式沒有什麼意義。
並且爲了記錄,此應用程序中的所有對象都是CLASSES。沒有任何結構。
更新#2:徽章類。
internal class Badge
: GameDataItem
{
public Badge()
: base()
{
}
public string AuthId { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public bool IsAccoladePower { get; set; }
public string RequiredBadges { get; set; }
public override string ToString()
{
return Text;
}
internal string ToXml()
{
var template = "<Badge value=\"{0}\" title=\"{1}\" category=\"{2}\" authid=\"{3}\" requires=\"{4}\" accolade=\"{5}\" description=\"{6}\" />";
return string.Format(template,
this.Value,
this.Text,
this.Category,
this.AuthId,
this.RequiredBadges,
this.IsAccoladePower,
this.Description);
}
}
以防萬一有人需要它,基類:
internal class GameDataItem
{
private string _text;
public string Text
{
get
{
return this._text;
}
set
{
this._text = value.Replace("<", "<")
.Replace(">", ">")
.Replace("&", "&");
}
}
public string Value { get; set; }
public override string ToString()
{
return Text + "=\"" + Value + "\"";
}
}
GetBadgeByText的外觀如何? – MarkPflug 2010-11-15 20:08:43
'this.GameData.GetBadgeByText'在做什麼? – 2010-11-15 20:09:01
「徽章」是結構還是類? – dtb 2010-11-15 20:09:12