示例#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));
 }