Serialization of two Dictionaries at once

前端 未结 1 430
深忆病人
深忆病人 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<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; }
        }
    }
    

    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)
提交回复
热议问题