/// <summary> /// Sanitizes the <paramref name="elementName"/> for any invalid characters. /// </summary> /// <param name="elementName">The name of the XML element to sanitize.</param> /// <returns>A sanitized <see cref="String"/> of <paramref name="elementName"/>.</returns> /// <remarks>Sanitation rules are as follows:<br/> /// 1. Names can contain letters, numbers, and these 4 characters: _ | : | . | -<br/> /// 2. Names cannot start with a number or punctuation character<br/> /// 3. Names cannot contain spaces<br/> /// </remarks> public static string SanitizeElementName(string elementName) { if (elementName == null) { throw new ArgumentNullException(nameof(elementName)); } if (StringUtility.StartsWith(elementName, StringComparison.OrdinalIgnoreCase, EnumerableUtility.Concat(StringConverter.ToEnumerable(StringUtility.NumericCharacters), new[] { "." }))) { int startIndex = 0; IList <char> numericsAndPunctual = new List <char>(EnumerableUtility.Concat(StringUtility.NumericCharacters.ToCharArray(), new[] { '.' })); foreach (char c in elementName) { if (numericsAndPunctual.Contains(c)) { startIndex++; continue; } break; } return(SanitizeElementName(elementName.Substring(startIndex))); } StringBuilder validElementName = new StringBuilder(); foreach (char c in elementName) { IList <char> validCharacters = new List <char>(EnumerableUtility.Concat(StringUtility.AlphanumericCharactersCaseSensitive.ToCharArray(), new[] { '_', ':', '.', '-' })); if (validCharacters.Contains(c)) { validElementName.Append(c); } } return(validElementName.ToString()); }
/// <summary> /// Gets a sorted (base-to-derived) sequence of ancestor-and-descendant-or-self types from the <paramref name="source"/>. /// </summary> /// <param name="source">The source type to locate ancestor-and-descendant-or-self types from.</param> /// <param name="assemblies">The assemblies to search for the <paramref name="source"/>.</param> /// <returns>An <see cref="IEnumerable{Type}"/> holding the ancestor-and-descendant-or-self types from the <paramref name="source"/>.</returns> public static IEnumerable <Type> GetAncestorAndDescendantsOrSelfTypes(Type source, params Assembly[] assemblies) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (assemblies == null) { throw new ArgumentNullException(nameof(assemblies)); } IEnumerable <Type> ancestorOrSelfTypes = GetAncestorOrSelfTypes(source); IEnumerable <Type> derivedOrSelfTypes = GetDescendantOrSelfTypes(source, assemblies); return(EnumerableUtility.SortDescending(EnumerableUtility.Concat(derivedOrSelfTypes, ancestorOrSelfTypes).Distinct(), new ReferenceComparer <Type>())); }
/// <summary> /// Combines a variable number of byte arrays into one byte array. /// </summary> /// <param name="bytes">The byte arrays to combine.</param> /// <returns>A variable number of <b>byte arrays</b> combined into one <b>byte array</b>.</returns> public static byte[] CombineByteArrays(params byte[][] bytes) { List <byte> combinedBytes = new List <byte>(EnumerableUtility.Concat <byte>(bytes)); return(combinedBytes.ToArray()); }
private static SqlException ParseException(Exception exception) { IEnumerable <Exception> exceptions = EnumerableUtility.Concat(EnumerableUtility.Yield(exception), ExceptionUtility.Flatten(exception)); return(EnumerableUtility.FindAll(exceptions, MatchSqlException).Cast <SqlException>().FirstOrDefault()); }