コード例 #1
0
ファイル: Utilities.cs プロジェクト: kant2002/infer
        /// <summary>
        /// Persists an object that controls its binary serialization to a file with the specified name.
        /// </summary>
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="fileMode">The mode used to open the file. Defaults to <see cref="FileMode.Create"/>.</param>
        /// <remarks>To load a saved learner, you can use the factory methods whose names start with LoadBackwardCompatible.</remarks>
        public static void SaveForwardCompatible(this ICustomSerializable obj, string fileName, FileMode fileMode = FileMode.Create)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            using (Stream stream = File.Open(fileName, fileMode))
            {
                if (fileName.EndsWith(".bin"))
                {
                    obj.SaveForwardCompatibleAsBinary(stream);
                }
                else
                {
                    obj.SaveForwardCompatibleAsText(stream);
                }
            }
        }