Пример #1
0
        public void Add()
        {
            var mruCollection = new MostRecentlyUsedCollection();

            var paths = new List <string>();

            for (int i = 0; i <= mruCollection.MaximumSize; i++)
            {
                paths.Add(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        @"c:\temp\myfile{0}.txt",
                        i));
            }

            for (int i = 0; i < paths.Count; i++)
            {
                mruCollection.Add(paths[i]);
                Assert.That(
                    mruCollection.Select(m => m.FilePath),
                    Is.EquivalentTo(
                        paths
                        .Skip(i >= mruCollection.MaximumSize ? i - mruCollection.MaximumSize + 1 : 0)
                        .Take(i >= mruCollection.MaximumSize ? mruCollection.MaximumSize : i + 1)
                        .Reverse()));
            }
        }
        /// <summary>
        /// Converts the given value object to the specified type, using the specified
        /// context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
        /// <param name="culture">The current culture.</param>
        /// <param name="value">The object to convert.</param>
        /// <param name="destinationType">The <see cref="Type"/> to convert the value parameter to.</param>
        /// <returns>The converted object.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="destinationType"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="NotSupportedException">The conversion cannot be performed.</exception>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType != typeof(string))
            {
                return(base.ConvertTo(context, culture, value, destinationType));
            }

            var mru = value as MostRecentlyUsedCollection;

            return(MostRecentlyUsedCollection.Serialize(culture, mru));
        }
        /// <summary>
        /// Converts the given object to the type of this converter, using the specified
        /// context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
        /// <param name="culture">The current culture.</param>
        /// <param name="value">The object to convert.</param>
        /// <returns>An object that represents the converted value.</returns>
        /// <exception cref="NotSupportedException">The conversion cannot be performed.</exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var text = value as string;

            if (text == null)
            {
                return(base.ConvertFrom(context, culture, value));
            }

            return(MostRecentlyUsedCollection.Deserialize(culture, text));
        }
        /// <summary>
        /// Serializes the collection to a string representation.
        /// </summary>
        /// <param name="culture">The culture that should be used for the deserialization.</param>
        /// <param name="serializedCollection">The collection that contains the most recently used information.</param>
        /// <returns>A new string with the serialized information.</returns>
        internal static string Serialize(CultureInfo culture, MostRecentlyUsedCollection serializedCollection)
        {
            var builder = new StringBuilder();

            builder.AppendLine(string.Format(culture, "{0}", serializedCollection.MaximumSize));
            foreach (var mru in serializedCollection)
            {
                builder.AppendLine(MostRecentlyUsed.Serialize(culture, mru));
            }

            return(builder.ToString());
        }
        /// <summary>
        /// Deserializes the collection from a string representation.
        /// </summary>
        /// <param name="culture">The culture that should be used for the deserialization.</param>
        /// <param name="serializedCollection">The string that contains the serialized most recently used information.</param>
        /// <returns>A new collection with the deserialized information.</returns>
        internal static MostRecentlyUsedCollection Deserialize(CultureInfo culture, string serializedCollection)
        {
            var result = new MostRecentlyUsedCollection();

            string[] parts = serializedCollection.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            result.MaximumSize = int.Parse(parts[0], culture);

            for (int i = 1; i < parts.Length; i++)
            {
                var item = MostRecentlyUsed.Deserialize(culture, parts[i]);
                result.m_Collection.Add(item);
            }

            return(result);
        }
Пример #6
0
        public void SerializeAndDeserialize()
        {
            var mruCollection = new MostRecentlyUsedCollection();

            for (int i = 0; i <= mruCollection.MaximumSize; i++)
            {
                mruCollection.Add(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        @"c:\temp\myfile{0}.txt",
                        i));
            }

            var converter              = new MostRecentlyUsedCollectionConverter();
            var serializedCollection   = converter.ConvertTo(null, CultureInfo.InvariantCulture, mruCollection, typeof(string));
            var deserializedCollection = converter.ConvertFrom(
                null,
                CultureInfo.InvariantCulture,
                serializedCollection) as MostRecentlyUsedCollection;

            Assert.That(deserializedCollection.Select(m => m.FilePath), Is.EquivalentTo(mruCollection.Select(m => m.FilePath)));
            Assert.That(deserializedCollection.Select(m => m.LastTimeOpened), Is.EquivalentTo(mruCollection.Select(m => m.LastTimeOpened)));
        }