为什么设置静态对象会导致我的方法调用中止?

发布于 2024-12-05 13:00:50 字数 1616 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-12-12 13:00:50

您在以下位置遇到 NullReferenceException

public static Dictionary<string, string> LongNames { get; set; }

因为自动生成的支持属性为 null。对于其他 Dictionary 变量和 List 变量也会发生这种情况。

您可以这样修复它:

private static Dictionary<string, string> longNames = new Dictionary<string, string>();

public static Dictionary<string, string> LongNames { get { return longNames; } set { longNames = value; } }

在这种情况下您甚至可以跳过设置器。

public class Gen2
{
    private static List<string> allFiNames = new List<string> allFiNames();
    public static List<string> AllFiNames { get { return allFiNames; } }

    private static Dictionary<string, string> longNames = new Dictionary<string, string>();
    public static Dictionary<string, string> LongNames { get { return longNames; } }

    private static Dictionary<string, string> apiKeys = new Dictionary<string, string>();
    public static Dictionary<string, string> ApiKeys { get { return apiKeys; } }

    private static Dictionary<string, string> connectStrings = Dictionary<string, string>();
    public static Dictionary<string, string> ConnectStrings { get { return connectStrings; } }
}

You are hitting a NullReferenceException in:

public static Dictionary<string, string> LongNames { get; set; }

because the auto-generated backing property is null. This also happens for the other Dictionary variables and the List variable.

You can fix it like this:

private static Dictionary<string, string> longNames = new Dictionary<string, string>();

public static Dictionary<string, string> LongNames { get { return longNames; } set { longNames = value; } }

You may even skip the setter in this case.

public class Gen2
{
    private static List<string> allFiNames = new List<string> allFiNames();
    public static List<string> AllFiNames { get { return allFiNames; } }

    private static Dictionary<string, string> longNames = new Dictionary<string, string>();
    public static Dictionary<string, string> LongNames { get { return longNames; } }

    private static Dictionary<string, string> apiKeys = new Dictionary<string, string>();
    public static Dictionary<string, string> ApiKeys { get { return apiKeys; } }

    private static Dictionary<string, string> connectStrings = Dictionary<string, string>();
    public static Dictionary<string, string> ConnectStrings { get { return connectStrings; } }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文