如何在foreach循环中使用KeyValuepair到达结构内容?

发布于 2025-02-12 17:51:28 字数 970 浏览 1 评论 0原文

这是我的方法,问题是Innerkey。值是struct,在txt文件中,我只有struct而不是结构内容的名称。 状态是嵌套对象是另一个dic。

如何在TXT文件中编写结构内容?

private void show2(Dictionary<string, object> state)
{
    TextWriter tw2 = new StreamWriter(savepath2);

    foreach (KeyValuePair<string, object> group1 in state)
    {

        Dictionary<string, object> group2 = (Dictionary<string, object>)group1.Value;

        foreach (KeyValuePair<string, object> innerKey in group2)
        {

            tw2.WriteLine("Id: {0} -- Type: {1} -- Value: {2}",group1.Key, innerKey.Key, innerKey.Value);
            tw2.Close();
        }

    }
    
}

还是有什么方法可以将嵌套字典作为对象作为输入文件,其中具有类似于序列化方法的流路径的输入文件?我不想序列化,因为我希望能够检查TXT内容:

private void SaveFile(object state)
{
    using (var stream = File.Open(savepath, FileMode.Create))
    {
        var formatter = new BinaryFormatter();

        formatter.Serialize(stream, state);
    }
}

This is my method and the problem is the innerKey. Value is struct and in txt file just I have the name of struct not the struct content. state is nested dic that the object is another dic.

How can i write struct content in my txt file?

private void show2(Dictionary<string, object> state)
{
    TextWriter tw2 = new StreamWriter(savepath2);

    foreach (KeyValuePair<string, object> group1 in state)
    {

        Dictionary<string, object> group2 = (Dictionary<string, object>)group1.Value;

        foreach (KeyValuePair<string, object> innerKey in group2)
        {

            tw2.WriteLine("Id: {0} -- Type: {1} -- Value: {2}",group1.Key, innerKey.Key, innerKey.Value);
            tw2.Close();
        }

    }
    
}

Or is there any way to write the nested dictionary as object as input file in a txt file with stream path similar to serialize method? I don´t want to serialize it because I want to be able to check txt content:

private void SaveFile(object state)
{
    using (var stream = File.Open(savepath, FileMode.Create))
    {
        var formatter = new BinaryFormatter();

        formatter.Serialize(stream, state);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文