/// <summary> /// Returns a generic <see cref="List{T}"/> /// of <see cref="ValueType"/> values parsed /// from a collection of strings using a specified /// <see cref="ConvertStrings.TryParseFromString{T}"/> delegate, /// ignoring any values which cannot be parsed /// and optionally having duplicates removed. /// </summary> /// <typeparam name="T"> /// The type of array to return, must /// be a <see cref="ValueType"/>. /// </typeparam> /// <param name="delimitedString"> /// A comma-delimited string containing /// <typeparamref name="T"/> values to parse. /// </param> /// <param name="tryParseDelegate"> /// A delegate method conforming to the /// <see cref="ConvertStrings.TryParseFromString{T}"/> /// signature, for optimized type conversion /// via a "TryParse" method common to many /// primitive <see cref="ValueType"/> classes. /// </param> /// <param name="removeDuplicates"> /// Whether to remove duplicate items in the array returned. /// Parameter is optional, default value is <c>false</c>. /// </param> /// <returns> /// A list of type <typeparamref name="T"/>. /// </returns> public static List <T> ToList <T>( this string delimitedString, ConvertStrings.TryParseFromString <T> tryParseDelegate, bool removeDuplicates = ConvertStructCollection.DefaultRemoveDuplicates) where T : struct { return(delimitedString.SplitDelimitedString().ToList( tryParseDelegate, removeDuplicates)); }
/// <summary> /// Returns a generic <see cref="IReadOnlyList{T}"/> /// of <see cref="ValueType"/> values parsed /// from a collection of strings using a specified /// <see cref="ConvertStrings.TryParseFromString{T}"/> delegate, /// ignoring any values which cannot be parsed /// and optionally having duplicates removed. /// </summary> /// <typeparam name="T"> /// The type of array to return, must /// be a <see cref="ValueType"/>. /// </typeparam> /// <param name="delimitedString"> /// A <paramref name="separator"/>-delimited string /// containing <typeparamref name="T"/> values to parse. /// </param> /// <param name="separator"> /// A string delimiter to use instead of a comma. /// </param> /// <param name="tryParseDelegate"> /// A delegate method conforming to the /// <see cref="ConvertStrings.TryParseFromString{T}"/> /// signature, for optimized type conversion /// via a "TryParse" method common to many /// primitive <see cref="ValueType"/> classes. /// </param> /// <param name="removeDuplicates"> /// Whether to remove duplicate items in the array returned. /// Parameter is optional, default value is <c>false</c>. /// </param> /// <returns> /// A list of type <typeparamref name="T"/>. /// </returns> public static IReadOnlyList <T> ToEnumerable <T>( this string delimitedString, string separator, ConvertStrings.TryParseFromString <T> tryParseDelegate, bool removeDuplicates = ConvertStructCollection.DefaultRemoveDuplicates) where T : struct { return(delimitedString.SplitDelimitedString(separator).ToEnumerable( tryParseDelegate, removeDuplicates)); }
/// <summary> /// Returns a typed array of <see cref="ValueType"/> values /// parsed from a collection of strings using a specified /// <see cref="ConvertStrings.TryParseFromString{T}"/> delegate, /// ignoring any values which cannot be parsed /// and optionally having duplicates removed. /// </summary> /// <typeparam name="T"> /// The type of array to return, must /// be a <see cref="ValueType"/>. /// </typeparam> /// <param name="delimitedString"> /// A <paramref name="separator"/>-delimited string /// containing <typeparamref name="T"/> values to parse. /// </param> /// <param name="separator"> /// A string delimiter to use instead of a comma. /// </param> /// <param name="tryParseDelegate"> /// A delegate method conforming to the /// <see cref="ConvertStrings.TryParseFromString{T}"/> /// signature, for optimized type conversion /// via a "TryParse" method common to many /// primitive <see cref="ValueType"/> classes. /// </param> /// <param name="removeDuplicates"> /// Whether to remove duplicate items in the array returned. /// Parameter is optional, default value is <c>false</c>. /// </param> /// <returns> /// An array of type <typeparamref name="T"/>. /// </returns> public static T[] ToArray <T>( this string delimitedString, string separator, ConvertStrings.TryParseFromString <T> tryParseDelegate, bool removeDuplicates = ConvertStructCollection.DefaultRemoveDuplicates) where T : struct { return(delimitedString.ToList( separator, tryParseDelegate, removeDuplicates).ToArray()); }
/// <summary> /// Gets the value from the <see cref="NameValueCollection"/>. /// </summary> /// <remarks> /// <para> /// This method seeks to make value retrieval from /// the <see cref="NameValueCollection"/> as /// simple as possible. Implemented using generics, /// you simply pass both the <see cref="Type"/> /// to return and the name of the item in the collection. /// </para> /// <para> /// In the event that the collection does not contain the /// named item or the proper conversion cannot be made, /// the default value of type <typeparamref name="T"/> is returned. /// </para> /// </remarks> /// <typeparam name="T"> /// The type of value to return. /// </typeparam> /// <param name="collection"> /// An instance of <see cref="NameValueCollection"/> /// from which to retrieve the value. /// </param> /// <param name="name"> /// The indexer for the <see cref="NameValueCollection"/>. /// </param> /// <param name="tryParseDelegate"> /// /// </param> /// <returns> /// The value from the <see cref="NameValueCollection"/> /// matching the <paramref name="name"/> parameter, /// coverted to the <see cref="Type"/> indicated by /// the generic type parameter <typeparamref name="T"/>, /// or the type's default value. /// </returns> public static T GetValue <T>( [NotNull] this NameValueCollection collection, [NotNull] ConvertStrings.TryParseFromString <T> tryParseDelegate, string name) where T : struct { Contract.Requires <ArgumentNullException>(collection != null); Contract.Requires <ArgumentNullException>(tryParseDelegate != null); return(GetValue(collection, tryParseDelegate, name, default(T))); }
/// <summary> /// Converts an item in a <see cref="NameValueCollection"/> /// to the generic type <typeparamref name="T"/>. /// A return value indicates whether the conversion succeeded or failed. /// </summary> /// <typeparam name="T"> /// The type of value to return. /// </typeparam> /// <param name="collection"> /// An instance of <see cref="NameValueCollection"/> to check for the value. /// </param> /// <param name="tryParseDelegate"> /// /// </param> /// <param name="name"> /// The indexer for the <see cref="NameValueCollection"/>. /// </param> /// <param name="result"> /// Returns the value from the <see cref="NameValueCollection"/> /// corresponding to the <paramref name="name"/> parameter /// converted to the generic type <typeparamref name="T"/>, /// or the default value of the generic type if the conversion failed. /// </param> /// <returns> /// True if the named value was converted successfully, otherwise false. /// </returns> public static bool TryParseValue <T>( [NotNull] this NameValueCollection collection, [NotNull] ConvertStrings.TryParseFromString <T> tryParseDelegate, string name, out T result) where T : struct { Contract.Requires <ArgumentNullException>(collection != null); Contract.Requires <ArgumentNullException>(tryParseDelegate != null); return(tryParseDelegate(collection[name], out result)); }
/// <summary> /// Gets the value from the <see cref="NameValueCollection"/>. /// </summary> /// <remarks> /// <para> /// This method seeks to make value retrieval from /// the <see cref="NameValueCollection"/> as /// simple as possible. Implemented using generics, /// you simply pass both the <see cref="Type"/> /// to return and the name of the item in the collection. /// </para> /// <para> /// In the event that the collection does not contain the /// named item or the proper conversion cannot be made, /// the value of the <paramref name="defaultReturn"/> /// parameter is returned. /// </para> /// </remarks> /// <typeparam name="T"> /// The type of value to return. /// </typeparam> /// <param name="collection"> /// An instance of <see cref="NameValueCollection"/> /// from which to retrieve the value. /// </param> /// <param name="tryParseDelegate"> /// /// </param> /// <param name="name"> /// The indexer for the <see cref="NameValueCollection"/>. /// </param> /// <param name="defaultReturn"> /// A value to return if the item could not be found or if the conversion fails. /// </param> /// <returns> /// The value from the <see cref="NameValueCollection"/> /// matching the <paramref name="name"/> parameter, /// coverted to the <see cref="Type"/> indicated by /// the generic type parameter <typeparamref name="T"/>, /// or the value of the <paramref name="defaultReturn"/> parameter. /// </returns> public static T GetValue <T>( [NotNull] this NameValueCollection collection, [NotNull] ConvertStrings.TryParseFromString <T> tryParseDelegate, string name, T defaultReturn) where T : struct { Contract.Requires <ArgumentNullException>(collection != null); Contract.Requires <ArgumentNullException>(tryParseDelegate != null); T result; if (!collection.TryParseValue(tryParseDelegate, name, out result)) { result = defaultReturn; } return(result); }