Serialization of two Dictionaries at once

前端 未结 1 433
深忆病人
深忆病人 2021-01-24 23:02

I have two classes as below and I\'m using them in two separate Dictionaries. How can I Serialize these two Dictionaries to a single file? my Serializa

1条回答
  •  一向
    一向 (楼主)
    2021-01-24 23:16

    Create a class marked with the Serializable attribute, containing instances of these two dictionaries:

    [Serializable]
    public class Building
    {
        public static Building Instance = new Building();
    
        public readonly Dictionary Beams = new Dictionary();
        public readonly Dictionary Columns = new Dictionary();
    
        public static Dictionary AllBeams
        {
            get { return Instance.Beams; }
        }
    }
    

    EDIT
    A Singleton pattern used to avoid the exception.
    In the other parts of the code use Building.Instance to access the dictionaries.
    EDIT2
    A static property introduced: Building.AllBeams. You can use it as shorthand.

    0 讨论(0)
提交回复
热议问题