C# 同时对两部词典进行序列化

C# 同时对两部词典进行序列化,c#,serialization,dictionary,C#,Serialization,Dictionary,我有两个类,如下所示,我在两个独立的词典中使用它们。如何将这两个词典序列化为一个文件?可以在以下链接中找到单个词典的序列化实现: 这是我的字典: var Dic1 = new Dictionary<string, Beam> { {"Beam1", new Beam{Name = "B1", Width = 10}}, {"Beam2", new Beam{Name = "B2", Width = 5}}, }; var Dic2 = new Dictionary

我有两个类,如下所示,我在两个独立的
词典中使用它们。如何将这两个词典序列化为一个文件?可以在以下链接中找到单个词典的序列化实现:

这是我的字典:

var Dic1 = new Dictionary<string, Beam>
{
    {"Beam1", new Beam{Name = "B1", Width = 10}},
    {"Beam2", new Beam{Name = "B2", Width = 5}},
};

var Dic2 = new Dictionary<string, Column>
{
    {"Column1", new Column{Name = "C1", Width = 10}},
    {"Column2", new Column{Name = "C2", Width = 5}},
};
[Serializable]
public class Building
{
    public static Building Instance = new Building();

    public readonly Dictionary<string, Beam> Beams = new Dictionary<string, Beam>();
    public readonly Dictionary<string, Column> Columns = new Dictionary<string, Column>();

    public static Dictionary<string, Beam> AllBeams
    {
        get { return Instance.Beams; }
    }
}
var Dic1=新字典
{
{“束1”,新束{Name=“B1”,宽度=10},
{“束2”,新束{Name=“B2”,宽度=5},
};
var Dic2=新字典
{
{“Column1”,新列{Name=“C1”,宽度=10},
{“Column2”,新列{Name=“C2”,宽度=5},
};
以下是我迄今为止编写的完整代码,但我遇到了一个例外:

[Serializable]
public static class Building
{
    public static Dictionary<string, Beam> Beams;
    public static Dictionary<string, Column> Columns;
}

[Serializable]
public class Beam
{
    public string Name { get; set; }
}

[Serializable]
public class Column
{
    public string Name { get; set; }
}

static class Program
{
    static void Main()
    {

        Building.Beams.Add("Beam1", new Beam { Name = "B1"});
        Building.Columns.Add("Column1", new Column { Name = "C1" });

        Serialize();
    }

    static void Serialize()
    {

        var fs = new FileStream("DataFile.dat", FileMode.Create);

        var formatter = new BinaryFormatter();
        try
        {
            // I'm getting an excepting here:
            // 'Savef.Building' is a 'type' but is used like a 'variable'
            formatter.Serialize(fs, Building);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }
    }
}
[可序列化]
公共静态课堂建设
{
公共静态字典梁;
公共静态字典列;
}
[可序列化]
公共级光束
{
公共字符串名称{get;set;}
}
[可序列化]
公共类专栏
{
公共字符串名称{get;set;}
}
静态类程序
{
静态void Main()
{
Building.Beams.Add(“Beam1”,新梁{Name=“B1”});
Building.Columns.Add(“Column1”,新列{Name=“C1”});
序列化();
}
静态void序列化()
{
var fs=newfilestream(“DataFile.dat”,FileMode.Create);
var formatter=新的二进制格式化程序();
尝试
{
//我在这里得到一个例外:
//“Savef.Building”是一个“类型”,但与“变量”一样使用
格式化程序。序列化(fs,构建);
}
捕获(序列化异常)
{
Console.WriteLine(“未能序列化。原因:+e.Message”);
投掷;
}
最后
{
fs.Close();
}
}
}

创建一个标有
Serializable
属性的类,其中包含这两个词典的实例:

var Dic1 = new Dictionary<string, Beam>
{
    {"Beam1", new Beam{Name = "B1", Width = 10}},
    {"Beam2", new Beam{Name = "B2", Width = 5}},
};

var Dic2 = new Dictionary<string, Column>
{
    {"Column1", new Column{Name = "C1", Width = 10}},
    {"Column2", new Column{Name = "C2", Width = 5}},
};
[Serializable]
public class Building
{
    public static Building Instance = new Building();

    public readonly Dictionary<string, Beam> Beams = new Dictionary<string, Beam>();
    public readonly Dictionary<string, Column> Columns = new Dictionary<string, Column>();

    public static Dictionary<string, Beam> AllBeams
    {
        get { return Instance.Beams; }
    }
}
[可序列化]
公共班级大楼
{
公共静态建筑实例=新建筑();
公共只读字典Beams=新字典();
public readonly Dictionary Columns=new Dictionary();
公共静态字典
{
获取{return Instance.Beams;}
}
}
编辑
用于避免异常的
单例模式。
在代码的其他部分,使用
Building.Instance
访问字典。
EDIT2

引入了一个
静态
属性:
Building.AllBeams
。您可以将其用作速记。

我遇到一个异常,可能是因为我定义了一个
静态
类?是的,您应该将一个实例传递给
格式化程序。序列化
。将
建筑
类转换为
单体
模式。我将更新答案。请参阅更新的答案。它已经被转换了。在代码的其余部分,只需用
Building.Instance
替换每次出现的
Building
。感谢Dmitry,这完全奏效了。我无法创建
静态
类的实例,对吗?我希望我的主要建筑类是静态的。在该方法中,我必须调用字典,如
Building.Instance.Beams
。我想把它们叫做
Building.Beams
A
static
类不能有实例。您可以使用不同的名称创建一个
静态
属性,以便能够以简短的形式调用
字典
。我已经更新了答案。