/// <summary>
        ///   <para>Generates specified number of random bytes.</para>
        /// </summary>
        /// <param name="self">Randomization object that is being extended.</param>
        /// <param name="count">Number of bytes to generate.</param>
        /// <returns>Array of randomly generated bytes. Length of array is equal to <paramref name="count"/>.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception>
        /// <exception cref="ArgumentException"></exception>
        /// <seealso cref="Random.NextBytes(byte[])"/>
        public static byte[] Bytes(this Random self, int count)
        {
            Assertion.NotNull(self);
            Assertion.True(count > 0);

            var numbers = new byte[count];

            self.NextBytes(numbers);
            return(numbers);
        }
示例#2
0
        /// <summary>
        ///   <para>Replaces all occurrences of different substrings with specified new values.</para>
        /// </summary>
        /// <param name="self">Source string where replacements are to be made.</param>
        /// <param name="from">Collection of substrings that should be replaced in source string. It must have the same number of elements as in <paramref name="to"/> sequence.</param>
        /// <param name="to">Collection of new replacement values. It must have the same number of elements as in <paramref name="from"/> sequence.</param>
        /// <returns>New string with performed replacements.</returns>
        /// <exception cref="ArgumentNullException">If either <paramref name="self"/>, <paramref name="from"/> or <paramref name="to"/> is a <c>null</c> reference.</exception>
        /// <exception cref="ArgumentException"></exception>
        /// <seealso cref="Replace(string, IDictionary{string, string})"/>
        public static string Replace(this string self, IEnumerable <string> from, IEnumerable <string> to)
        {
            Assertion.NotNull(self);
            Assertion.NotNull(from);
            Assertion.NotNull(to);
            Assertion.True(from.Count() == to.Count());

            var sb = new StringBuilder(self);

            for (var i = 0; i < from.Count(); i++)
            {
                sb.Replace(from.ElementAt(i), to.ElementAt(i));
            }
            return(sb.ToString());
        }
示例#3
0
        /// <summary>
        ///   <para>Returns a collection of values of <see cref="DescriptionAttribute"/> for a given enumeraton type.</para>
        /// </summary>
        /// <typeparam name="T">Type of enumeration.</typeparam>
        /// <returns>Collection of descriptions for a given enumeration of type <typeparamref name="T"/>.</returns>
        /// <seealso cref="DescriptionAttribute"/>
        /// <seealso cref="Description(Enum)"/>
        public static IEnumerable <string> Descriptions <T>() where T : struct
        {
            Assertion.True(typeof(T).IsEnum);

            var descriptions = new List <string>();
            var type         = typeof(T);
            var names        = Enum.GetNames(type);

            names.Each(name =>
            {
                var enumeration = Enum.Parse(type, name, true);
                var description = enumeration.To <Enum>().Description();
                descriptions.Add(description.IsEmpty() ? name : description);
            });

            return(descriptions);
        }
 public void assert_true()
 {
     Assertion.True(true);
     Assert.Throws <ArgumentException>(() => Assertion.True(false));
 }