更新:我已经解决了我遇到的问题,但我不知道为什么该错误会生成它所做的堆栈跟踪。堆栈跟踪将我引向完全错误的方向。如果有人能解释这里发生了什么,我将不胜感激(并将您的答案标记为已接受)。请注意,我的原始帖子已被删除。
我有以下课程。它的不相关部分已被删除:
class ClassName {
private string[] _accountTypes = new string[2] {"ECOM", "MOTO"};
private Dictionary<string, string> _settleDueDateDictionary = new Dictionary<string, string>() {
{"0", "Process immediately."},
{"1", "Wait 1 day"},
{"2", "Wait 2 days"},
{"3", "Wait 3 days"},
{"4", "Wait 4 days"},
{"5", "Wait 5 days"},
{"6", "Wait 6 days"},
{"7", "Wait 7 days"},
};
private string _settleDueDate;
private string _accountTypeDescription;
public string SettleDueDate
{
get
{
DateTime today = DateTime.Today;
long settleDueDate = Convert.ToInt64(_settleDueDate);
return today.AddDays(settleDueDate).ToString("MM/dd/yyyy");
}
set
{
if (!_settleDueDateDictionary.ContainsKey(value)) {
// TODO - handle
}
_settleDueDate = value;
}
}
public string AccountTypeDescription
{
get {
//return AccountTypeDescription; // This would cause infinite recursion (not referring to backing property).
return _accountTypeDescription; // This fixed the StackOverflowException I was faxed with
}
set
{
if (!_accountTypes.Contains(value))
{
// TODO - handle
}
_accountTypeDescription = value;
}
}
}
我也有这个类,它采用上面类的一个实例,并使用实例中的值创建了一个 XML 字符串:
class SecondClass
{
private ClassName classnameInstance;
public SecondClass(ClassName instance)
{
classnameInstance = instance;
}
public string PrepareRequest(XMLWriter writer)
{
writer.WriteElementString("accounttypedescription", classnameInstance.AccountTypeDescription);
}
}
这是生成堆栈跟踪的客户端代码:
STPPData STPP = new STPPData();
STPP.SiteReference = _secureTradingWebServicesPaymentSettings.SiteReference;
STPP.Alias = _secureTradingWebServicesPaymentSettings.Alias;
STPP.SettleDueDate = Convert.ToString(_secureTradingWebServicesPaymentSettings.SettleDueDate);
STPP.SettleStatus = _secureTradingWebServicesPaymentSettings.SettleStatus;
STPPXml STPPXml = new STPPXml(STPP);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Async = false;
var builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder, settings))
{
string xmlRequest = STPPXml.PrepareRequest(writer);
}
最后,这是堆栈跟踪:
mscorlib.dll!string.GetHashCode()
mscorlib.dll!System.Collections.Generic.GenericEqualityComparer<System.__Canon>.GetHashCode(SYstem.__Canon obj)
mscorlib.dll!System.Collections.Generic.Dictionary<string,string>.FindEntry(string key)
mscorlib.dll!System.Collections.Generic.Dictionary<System.__Canon,System.__Canon>.ContainsKey(System.__Canon key)
ClassName.SettleDueDate.set(string value)
ClassName.SettleDueDate.set(string value)
ClassName.SettleDueDate.set(string value)
// Infinite recursion of this call
这个堆栈跟踪让我相信我错误地为 STPP.SettleDueDate 实现了 getter/setter。我检查了它们并且支持变量等是正确的(我理解 getter/setter 中循环的常见原因)。进一步调试表明,调用此行时实际上生成了堆栈跟踪PrepareRequest()
:
writer.WriteElementString("accounttypedescription", STPPData.AccountTypeDescription);
我发现我错误地实现了 STPPData.AccountTypeDescription 的 getter,因为我创建了一个在 setter 中使用的支持属性,但我没有在 getter 中使用支持属性:
public string AccountTypeDescription
{
get {
//return AccountTypeDescription; // This would cause infinite recursion.
return _accountTypeDescription; // This fixed the StackOverflowException
}
// setter omitted for clarity (it is in the examples above)
}
我的问题是:
当错误实际上在 AccountTypeDescription.get() 中时,为什么 StackOverflowException 的堆栈跟踪将我指向 SettleDueDate.set()?
注意:我是 C# 新手,来自 LAMP 背景。我已经稍微简化了代码,但我认为我没有删除任何重要的东西。