为什么设置静态对象会导致我的方法调用中止?
我有一个包含 4 个选项卡的 WPF 应用程序。 Tab1 是加载应用程序的第一个选项卡。我有一个 Tab1Load 方法(选项卡的名称),如下所示:
private void Tab1Load(object sender, RoutedEventArgs e)
{
myConfig.LoadConfigurationData();
XiphosDB.DataContext = Xiphos.XiphosDbNames;
}
myConfig 在 Window1.xaml.cs 文件顶部声明
LoadConfigData myConfig = new LoadConfigData();
LoadConfigData 如下所示:
public void LoadConfigurationData()
{
LoadGen2Data();
LoadXiphosData();
LoadTestConsumerData();
}
第一个方法调用 LoadGen2Data 一直运行到最后点它为静态对象设置一个值。调用是:
var count = 0;
foreach (var name in Gen2.allFiNames)
{
Gen2.ApiKeys.Add(name, APIKeys[count]);
Gen2.ConnectStrings.Add(name, connectStrings[count]);
Gen2.LongNames.Add(name, LongNames[count]);
count++;
}
Gen2 声明是:
public class Gen2
{
public static List<string> allFiNames { get; set; }
public static Dictionary<string, string> LongNames { get; set; }
public static Dictionary<string, string> ApiKeys { get; set; }
public static Dictionary<string, string> ConnectStrings { get; set; }
}
调用 Gen2.ApiKeys.Add 后,应用程序会跳过它所在的方法 (LoadGen2Data),而不调用其余的 Add 语句,然后退出 LoadConfigurationData 方法而不调用其余两个方法(LoadXiphosData 和 LoadTestConsumerData)。
进入 foreach 循环的顶部,我已经验证了“name”值已填充,并且 APIKeys[count] 提供了合法值(GUID)。
我没有收到任何错误。没有捕获其他调试点,我可以毫无问题地在应用程序 GUI 中移动(并且没有数据)。
知道问题是什么吗?
谢谢,
杰森
I have a WPF application which contains 4 tabs. Tab1 is the first tab loaded with the application. I have a Tab1Load method (the name of the tab) which looks like:
private void Tab1Load(object sender, RoutedEventArgs e)
{
myConfig.LoadConfigurationData();
XiphosDB.DataContext = Xiphos.XiphosDbNames;
}
myConfig was declared at the top of the Window1.xaml.cs file
LoadConfigData myConfig = new LoadConfigData();
LoadConfigData looks like:
public void LoadConfigurationData()
{
LoadGen2Data();
LoadXiphosData();
LoadTestConsumerData();
}
The first method call, LoadGen2Data, runs all the way to the end at which point it sets a value to static object. The call is:
var count = 0;
foreach (var name in Gen2.allFiNames)
{
Gen2.ApiKeys.Add(name, APIKeys[count]);
Gen2.ConnectStrings.Add(name, connectStrings[count]);
Gen2.LongNames.Add(name, LongNames[count]);
count++;
}
The Gen2 declaration is:
public class Gen2
{
public static List<string> allFiNames { get; set; }
public static Dictionary<string, string> LongNames { get; set; }
public static Dictionary<string, string> ApiKeys { get; set; }
public static Dictionary<string, string> ConnectStrings { get; set; }
}
Right after Gen2.ApiKeys.Add is called, the application skips out of the method it is in (LoadGen2Data) without calling the rest of the Add statements and then exits out of the LoadConfigurationData method without calling the remaining two methods (LoadXiphosData and LoadTestConsumerData).
Stepping into the top of the foreach loop I have verified that the "name" value is populated and APIKeys[count] provides a legitimate value (a GUID).
I do not receive any errors. No other debug points are caught and I can move around the application GUI with no problems (and no data).
Any idea what the problem is?
Thanks,
Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在以下位置遇到
NullReferenceException
:因为自动生成的支持属性为
null
。对于其他Dictionary
变量和List
变量也会发生这种情况。您可以这样修复它:
在这种情况下您甚至可以跳过设置器。
You are hitting a
NullReferenceException
in:because the auto-generated backing property is
null
. This also happens for the otherDictionary
variables and theList
variable.You can fix it like this:
You may even skip the setter in this case.