/// <summary>
        ///   <para>Converts array of characters into array of bytes, using specified encoding.</para>
        /// </summary>
        /// <param name="self">Source array of characters.</param>
        /// <param name="encoding">Encoding to be used for transforming between <see cref="char"/> at its <see cref="byte"/> equivalent. If not specified, uses <see cref="Encoding.UTF8"/> encoding.</param>
        /// <returns>Array of bytes which represents <paramref name="self"/> array in <paramref name="encoding"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="Encoding.GetBytes(char[])"/>
        public static byte[] Bytes(this char[] self, Encoding encoding = null)
        {
            Assertion.NotNull(self);

            return((encoding ?? Encoding.UTF8).GetBytes(self));
        }
        /// <summary>
        ///   <para>Converts array of bytes into HEX-encoded string.</para>
        /// </summary>
        /// <param name="self">Bytes to convert to HEX string.</param>
        /// <returns>HEX string representation of <paramref name="self"/> array.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="BitConverter.ToString(byte[])"/>
        /// <seealso cref="StringExtensions.Hex(string)"/>
        public static string Hex(this byte[] self)
        {
            Assertion.NotNull(self);

            return(BitConverter.ToString(self).Replace("-", ""));
        }
        /// <summary>
        ///   <para>Converts sequence of elements into a set collection type.</para>
        /// </summary>
        /// <typeparam name="T">Type of elements in a sequence.</typeparam>
        /// <param name="self">Source sequence of elements.</param>
        /// <returns>Set collection which contains elements from <paramref name="self"/> sequence without dublicates. Order of elements in a set is not guaranteed to be the same as returned by <paramref name="self"/>'s enumerator.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        public static ISet <T> ToSet <T>(this IEnumerable <T> self)
        {
            Assertion.NotNull(self);

            return(new SortedSet <T>(self));
        }
        /// <summary>
        ///   <para>Returns BASE64-encoded representation of a bytes sequence.</para>
        /// </summary>
        /// <param name="self">Bytes to convert to BASE64 encoding.</param>
        /// <returns>BASE64 string representation of <paramref name="self"/> array.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="System.Convert.ToBase64String(byte[])"/>
        public static string Base64(this byte[] self)
        {
            Assertion.NotNull(self);

            return(System.Convert.ToBase64String(self));
        }
        /// <summary>
        ///   <para>Converts array of bytes into a string, using specified encoding.</para>
        /// </summary>
        /// <param name="self">Source array of bytes.</param>
        /// <param name="encoding">Encoding to be used for transforming between <see cref="byte"/> at its <see cref="char"/> equivalent. If not specified, uses <see cref="Encoding.UTF8"/> encoding.</param>
        /// <returns>Array of characters as a string which represents <paramref name="self"/> array in <paramref name="encoding"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="String(char[])"/>
        /// <seealso cref="Encoding.GetString(byte[], int, int)"/>
        public static string String(this byte[] self, Encoding encoding = null)
        {
            Assertion.NotNull(self);

            return((encoding ?? Encoding.UTF8).GetString(self, 0, self.Length));
        }
        /// <summary>
        ///   <para>Concatenates all elements in a sequence into a string, using comma as a separator and placing the result inside a square brackets.</para>
        /// </summary>
        /// <typeparam name="T">Type of elements in a sequence.</typeparam>
        /// <param name="self">Source sequence of elements.</param>
        /// <returns>String which is formed from string representation of each element in a <paramref name="self"/> with a comma-character separator between them, all inside square brackets.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self" /> is a <c>null</c> reference.</exception>
        public static string ToListString <T>(this IEnumerable <T> self)
        {
            Assertion.NotNull(self);

            return($"[{self.Join(", ")}]");
        }
 public void assert_true()
 {
     Assertion.True(true);
     Assert.Throws <ArgumentException>(() => Assertion.True(false));
 }
        /// <summary>
        ///   <para>Returns string representation of specified array of characters.</para>
        /// </summary>
        /// <param name="self">Source array of characters.</param>
        /// <returns>String which is formed from contents of <paramref name="self"/> array.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="String(byte[], Encoding)"/>
        public static string String(this char[] self)
        {
            Assertion.NotNull(self);

            return(new string(self));
        }
 public void assert_null()
 {
     Assertion.Null(null);
     Assert.Throws <ArgumentException>(() => Assertion.Null(new object()));
 }
示例#10
0
        /// <summary>
        ///   <para>Deserializes XML string text into object of specified type.</para>
        /// </summary>
        /// <typeparam name="T">Type of object which is to be the result of deserialization process.</typeparam>
        /// <param name="self">XML data for deserialization.</param>
        /// <param name="types">Additional types to be used by <see cref="XmlSerializer"/> for deserialization purposes.</param>
        /// <returns>Deserialized XML contents of <paramref name="self"/> string as the object (or objects graph with a root element) of type <typeparamref name="T"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <exception cref="ArgumentException">If <paramref name="self"/> is <see cref="string.Empty"/> string.</exception>
        /// <seealso cref="XmlSerializer"/>
        public static T AsXml <T>(this string self, params Type[] types)
        {
            Assertion.NotEmpty(self);

            return(new StringReader(self).AsXml <T>(true, types));
        }
示例#11
0
        /// <summary>
        ///   <para>Translates XML content from specified <see cref="XmlReader"/> into a dictionary.</para>
        ///   <para>Attributes in XML document are translated to string keys and values, nodes become dictionaries with string keys themselves.</para>
        /// </summary>
        /// <param name="self"><see cref="XmlReader"/> used for reading of XML data which is to be converted to <see cref="IDictionary{TKey,TValue}"/> instance.</param>
        /// <returns>Dictionary that follows the structure of XML data from <see cref="XmlReader"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        public static IDictionary <string, object> Dictionary(this XmlReader self)
        {
            Assertion.NotNull(self);

            return(System.Xml.Linq.XDocument.Load(self).Dictionary());
        }
示例#12
0
        /// <summary>
        ///   <para>Translates specified <see cref="XDocument"/> into a dictionary.</para>
        ///   <para>Attributes in XML document are translated to string keys and values, nodes become dictionaries with string keys themselves.</para>
        /// </summary>
        /// <param name="self"><see cref="XDocument"/>, whose structure is to be converted to <see cref="IDictionary{TKey,TValue}"/> instance.</param>
        /// <returns>Dictionary that follows the structure of <see cref="XDocument"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="Dictionary(XElement)"/>
        public static IDictionary <string, object> Dictionary(this XDocument self)
        {
            Assertion.NotNull(self);

            return(self.Root.Dictionary());
        }
示例#13
0
        /// <summary>
        ///   <para>Deserializes XML data from <see cref="XmlReader"/> into object of specified type.</para>
        /// </summary>
        /// <typeparam name="T">Type of object which is to be the result of deserialization process.</typeparam>
        /// <param name="self"><see cref="XmlReader"/> used for retrieving XML data for deserialization.</param>
        /// <param name="types">Additional types to be used by <see cref="XmlSerializer"/> for deserialization purposes.</param>
        /// <returns>Deserialized XML contents from a <paramref name="self"/> as the object (or objects graph with a root element) of <paramref name="self"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <seealso cref="XmlSerializer"/>
        /// <seealso cref="Deserialize(XmlReader, Type, IEnumerable{Type})"/>
        public static T Deserialize <T>(this XmlReader self, IEnumerable <Type> types = null)
        {
            Assertion.NotNull(self);

            return(self.Deserialize(typeof(T), types).As <T>());
        }