示例#1
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Loads an instance of T from a file.  The file may contain a
        /// serialized form of an editable instance or it may be a text file
        /// that needs parsing.
        /// </summary>
        public static T Load <T>(string path,
                                 ITextParser <T> parser)
        {
            if (Path.GetExtension(path) == FileExtension)
            {
                //  Deserialize an editable instance from the file
                //  Binary serialization:
                IFormatter formatter = new BinaryFormatter();
                Stream     stream    = new FileStream(path, FileMode.Open,
                                                      FileAccess.Read, FileShare.Read);
                using (stream) {
                    IEditable <T> editableObject = (IEditable <T>)formatter.Deserialize(stream);
                    if (!editableObject.IsComplete)
                    {
                        throw new System.ApplicationException("Not complete T");
                    }
                    return(editableObject.GetComplete());
                }
            }
            else
            {
                LineReader reader = OpenTextFile(path);
                try {
                    return(parser.Parse(reader));
                }
                finally {
                    reader.Close();
                }
            }
        }