代码之家  ›  专栏  ›  技术社区  ›  Imanol Zubiaurre

如何保存/加载到具有不可序列化属性的磁盘类?

  •  1
  • Imanol Zubiaurre  · 技术社区  · 7 年前

    我有一个类,必须保存到磁盘,然后加载每次应用程序初始化。我不在乎创建的文件是Json、XML还是二进制文件。这是一节课:

        public class PhotoSpot_v01
        {
            public int      ID { get; set; }
            public string   Title { get; set; }
            public string   Description { get; set; }
            public int      Rating { get; set; }
            public bool     Reminder { get; set; }
            public bool     Toast { get; set; }
    
            private double   Latitude { get; set; }
            private double   Longitude { get; set; }
            private double   Altitude { get; set; }
    
            public GoogleMapsAPI.TimeZone TimeZone { get; set; }
    
            public globalVars.Tags MainTag { get; set; }
    
            public version Version { get; set; }
    
            public Milestone_v01    Milestone { get; set; }
            public List<Image_v01>  Images { get; set; }
            public List<URL_v01>    URLs { get; set; }
            public List<globalVars.Tag>    Tags { get; set; }
            public List<Date_v01>   Dates { get; set; }}
    

    在这里,类和枚举。。。在主类中使用:

        public class Milestone_v01
        {
            public bool     freeMilestone { get; set; }
            public string   Title { get; set; }
            public double   Latitude { get; set; }
            public double   Longitude { get; set; }
            public double   Altitude { get; set; }
            public globalVars.Astro Astro { get; set; }
        }
    
        public enum Astro
        {
            Sun, Moon, Night, Any
        }
    
    
        public class Image_v01
        {
            public string imageURI { get; set; }
            public bool   isDownloaded { get; set; }
        }
    
        public class URL_v01
        {
            public string sURL { get; set; }
        }
    
        public class Tag_v01
        {
            public globalVars.Tags TagName { get; set; }
        }
    
        public class Date_v01
        {
            public DateTime DateStart { get; set; }
            public DateTime DateEnd { get; set; }
        }
    

    3 回复  |  直到 7 年前
        1
  •  3
  •   Marc Gravell    7 年前

    实际上,我在您的模型中没有看到任何“不可序列化”的东西(看起来任何主要的模型都可以很好地使用它—尤其是 XmlSerializer DateTime . 但是:如果真的有 是

    1. 大多数序列化工具/库都支持某种类型的“自定义序列化程序”API,以防某些东西无法开箱即用;这通常需要您学习 太多关于底层序列化体系结构的信息(例如: IXmlSerializable.ReadXml 不要
    2. 更简单: . 这个问题通常发生在人们坚持要序列化他们的 ,但是。。。这只是把事情搞砸了:你最终会和序列化程序打起来。更简单和更易维护的是构建一个单独的模型 喜欢 之间 这两种模式。

    在2的某些情况下,序列化工具甚至可能支持“代理”概念,因此您只需要为 类型,当它们变得笨拙时-但是可以将主要模型用于大多数其他事情。例如,protobuf net允许您这样做,只要在两个类型之间的两个方向上都有一个转换操作(隐式或显式),那么您就可以有一个 Foo 类型,和 FooSerialization 福 福 ,但哪一个更“简单”(因为缺少更好的词)-使用明显的简单类型(例如,可能使用 byte[] Image / Bitmap

        2
  •  0
  •   Manoj Choudhari    7 年前

    我建议用newtonsoft.json文件.

        3
  •  0
  •   Imanol Zubiaurre    7 年前

    在上面的对话中回应Marc gravel说我不能发布格式化的代码。。。

    因此,如果我在Date\u v01类中对DateTime做了这样的更改,它可以在给定的问题代码中看到(在Geopoint的扩展中也可以看到):

        public class Date_v01
        {
    
            public double DateStart;
            public double DateEnd;
    
            public DateTime DateTimeStart()
            {
                return CoreTime.JD2DateTime(DateStart);
            }
    
            public DateTime DateTimeEnd()
            {
                return CoreTime.JD2DateTime(DateEnd);
            }
    
            //public DateTime DateStart { get; set; }
            //public DateTime DateEnd { get; set; }
        }
    

    序列化问题应该解决,对吗?