예제 #1
0
        /// <summary>
        /// Loads a plain text settings file using the specified path.
        /// </summary>
        /// <param name="path">The relative or absolute path to the settings file.</param>
        /// <param name="settings">The <see cref="PlainTextReaderSettings"/> that the <see cref="PlainTextReader"/> should use when loading a settings file.</param>
        /// <returns>Returns a new instance of the class <see cref="T"/> with variables loaded from the settings file.</returns>
        public static T Load(string path, PlainTextReaderSettings settings)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            string textPath      = FixPathExtension(path);
            bool   omitExtension = false;

            if (!File.Exists(textPath))
            {
                textPath      = path;
                omitExtension = true;
            }
            if (!File.Exists(textPath))
            {
                throw new FileNotFoundException(string.Format(Resources.SettingsExceptionStrings.SettingsNotFound, textPath), textPath);
            }

            using (FileStream stream = new FileStream(path, FileMode.Open)) {
                using (PlainTextReader reader = PlainTextReader.Create(stream, settings)) {
                    T instance = new PlainTextSerializer().Deserialize <T>(reader);
                    instance.SavePath      = path;
                    instance.OmitExtension = omitExtension;
                    return(instance);
                }
            }
        }
예제 #2
0
        public void Deserialize_TextMessage_CorrectResult()
        {
            var serializer = new PlainTextSerializer();

            var result = serializer.Deserialize(new TextMessage {
                Body = MessageText
            });

            Assert.That(result == MessageText);
        }
예제 #3
0
        public void Serialize_CorrectText_TextMessageCreated()
        {
            var serializer = new PlainTextSerializer();

            var result = serializer.Serialize(MessageText);

            var message = (TextMessage)result;

            Assert.That(message.Body == MessageText);
        }
예제 #4
0
        public void Deserialize_BytesMessage_Exception()
        {
            var serializer = new PlainTextSerializer();

            Assert.Throws <InvalidCastException>(() => serializer.Deserialize(new BytesMessage()));
        }