コード例 #1
0
        public static void Serialise <T>(string fileName, ESerialisationFlags format, T value)
        {
            fileName = GetFileName(fileName);

            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                Serialise <T>(fs, format, value);
            }
        }
コード例 #2
0
        public static void Serialise <T>(Stream fs, ESerialisationFlags format, T value)
        {
            Stream fsu;

            switch (format & ESerialisationFlags._CompressMask)
            {
            case ESerialisationFlags.GZip:
                fsu = new GZipStream(fs, CompressionMode.Compress);
                break;

            case ESerialisationFlags.NoCompression:
                fsu = fs;
                break;

            default:
                throw new SwitchException("format", format);
            }

            try
            {
                switch (format & ESerialisationFlags._SerialiserMask)
                {
                case ESerialisationFlags.Binary:
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fsu, value);
                    break;

                case ESerialisationFlags.Xml:
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    xs.Serialize(fsu, value);
                    break;

                case ESerialisationFlags.Ini:
                    IniSerialiser.Serialise <T>(fsu, value);
                    break;

                default:
                    throw new SwitchException("format", format);
                }
            }
            finally
            {
                switch (format & ESerialisationFlags._CompressMask)
                {
                case ESerialisationFlags.GZip:
                    fsu.Dispose();
                    break;

                case ESerialisationFlags.NoCompression:
                    break;

                default:
                    throw new SwitchException(format);
                }
            }
        }
コード例 #3
0
        public static T Deserialise <T>(FileStream fs, ESerialisationFlags format)
        {
            Stream fsu;

            switch (format & ESerialisationFlags._CompressMask)
            {
            case ESerialisationFlags.GZip:
                fsu = new GZipStream(fs, CompressionMode.Decompress);
                break;

            case ESerialisationFlags.NoCompression:
                fsu = fs;
                break;

            default:
                throw new SwitchException(format);
            }

            try
            {
                switch (format & ESerialisationFlags._SerialiserMask)
                {
                case ESerialisationFlags.Binary:
                    BinaryFormatter bf = new BinaryFormatter();
                    return((T)bf.Deserialize(fsu));

                case ESerialisationFlags.Xml:
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    return((T)xs.Deserialize(fsu));

                case ESerialisationFlags.Ini:
                    return(IniSerialiser.Deserialise <T>(fsu));

                default:
                    throw new SwitchException("format", format);
                }
            }
            finally
            {
                switch (format & ESerialisationFlags._CompressMask)
                {
                case ESerialisationFlags.GZip:
                    fsu.Dispose();
                    break;

                case ESerialisationFlags.NoCompression:
                    break;

                default:
                    throw new SwitchException(format);
                }
            }
        }
コード例 #4
0
        public static T Deserialise <T>(string fileName, ESerialisationFlags format)
        {
            fileName = GetFileName(fileName);

            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("Deserialise file not found.", fileName);
            }

            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                return(Deserialise <T>(fs, format));
            }
        }
コード例 #5
0
        public static T DeserialiseOrDefault <T>(string fileName, ESerialisationFlags format, Func <T> defaultProvider)
        {
            if (File.Exists(fileName))
            {
                try
                {
                    return(Deserialise <T>(fileName, format));
                }
                catch
                {
                    // Ignore
                }
            }

            if (defaultProvider == null)
            {
                return(default(T));
            }
            else
            {
                return(defaultProvider());
            }
        }
コード例 #6
0
 public static T DeserialiseOrDefault <T>(string fileName, ESerialisationFlags format, T defaultValue)
 {
     return(DeserialiseOrDefault <T>(fileName, format, () => defaultValue));
 }
コード例 #7
0
 public static T DeserialiseOrDefault <T>(string fileName, ESerialisationFlags format)
 {
     return(DeserialiseOrDefault <T>(fileName, format, (Func <T>)null));
 }