Binary/Soap formatter code

        public static void SerializeToBin(this object objectGraph, FileInfo target)
        {
            if (target.Exists)
            {
                target.Delete();
            }
            using (var fs = target.Create())
            {
                var bs = new BinaryFormatter();
                bs.Serialize(fs, objectGraph);
            }
        }

        public static T DeserializeFromBin(this FileInfo source)
        {
            if (!source.Exists)
            {
                return default(T);
            }
            using (var fs = source.OpenRead())
            {
                var bs = new BinaryFormatter();
                return (T)bs.Deserialize(fs);
            }
        }

        public static void SerializeToSoap(this object objectGraph, FileInfo target)
        {
            if (target.Exists)
            {
                target.Delete();
            }
            using (var fs = target.Create())
            {
                var bs = new SoapFormatter();
                bs.Serialize(fs, objectGraph);
            }
        }

        public static T DeserializeFromSoap(this FileInfo source)
        {
            if (!source.Exists)
            {
                return default(T);
            }
            using (var fs = source.OpenRead())
            {
                var bs = new SoapFormatter();
                return (T)bs.Deserialize(fs);
            }
        }


댓글

이 블로그의 인기 게시물

Oracle NLS_DATE_FORMAT 변경

Stop console process using Ctrl+C.

Alternative to IValueConvert, QuickConverter