Created
June 17, 2010 15:25
-
-
Save KevM/442266 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class BasicExtensions | |
| { | |
| public static void WriteToConsole(this string stringValue) | |
| { | |
| Console.WriteLine(stringValue); | |
| } | |
| /// <summary> | |
| /// Fills the format string with the provided arguments | |
| /// </summary> | |
| public static string ToFormat(this string format, params object[] args) | |
| { | |
| return String.Format(format, args); | |
| } | |
| /// <summary> | |
| /// Returns true if the value being tested is not null and not an empty string | |
| /// </summary> | |
| /// <param name="stringValue">The value to test</param> | |
| /// <returns></returns> | |
| public static bool IsNotEmpty(this string stringValue) | |
| { | |
| return !String.IsNullOrEmpty(stringValue); | |
| } | |
| /// <summary> | |
| /// Returns true if the value being tested is null or an empty string | |
| /// </summary> | |
| /// <param name="stringValue">The value to test</param> | |
| /// <returns></returns> | |
| public static bool IsEmpty(this string stringValue) | |
| { | |
| return String.IsNullOrEmpty(stringValue); | |
| } | |
| /// <summary> | |
| /// Returns true if the value being tested is null or contains no items | |
| /// </summary> | |
| /// <param name="enumerable">The value to test</param> | |
| /// <returns></returns> | |
| public static bool IsEmpty<T>(this IEnumerable<T> enumerable) | |
| { | |
| return (enumerable == null || !enumerable.Any()); | |
| } | |
| /// <summary> | |
| /// Performs an action with a counter for each item in a sequence and provides | |
| /// </summary> | |
| /// <typeparam name="T">The type of the items in the sequence</typeparam> | |
| /// <param name="values">The sequence to iterate</param> | |
| /// <param name="eachAction">The action to performa on each item</param> | |
| /// <returns></returns> | |
| public static void Each<T>(this IEnumerable<T> values, Action<T, int> eachAction) | |
| { | |
| var index = 0; | |
| foreach (var item in values) | |
| { | |
| eachAction(item, index++); | |
| } | |
| } | |
| /// <summary> | |
| /// Performs an action for each item in a sequence. Good for one-line foreach statements. | |
| /// </summary> | |
| /// <typeparam name="T">The type of the items in the sequence</typeparam> | |
| /// <param name="values">The sequence to iterate</param> | |
| /// <param name="eachAction">The action to performa on each item</param> | |
| /// <returns></returns> | |
| public static void Each<T>(this IEnumerable<T> values, Action<T> eachAction) | |
| { | |
| foreach (var item in values) | |
| { | |
| eachAction(item); | |
| } | |
| } | |
| /// <summary> | |
| /// Performs an action for each item in a sequence. Good for one-line foreach statements. | |
| /// </summary> | |
| /// <param name="values">The sequence to iterate</param> | |
| /// <param name="eachAction">The action to performa on each item</param> | |
| /// <returns>The original sequence so that calls can be chained</returns> | |
| public static IEnumerable Each(this IEnumerable values, Action<object> eachAction) | |
| { | |
| foreach (var item in values) | |
| { | |
| eachAction(item); | |
| yield return item; | |
| } | |
| } | |
| /// <summary> | |
| /// Sets the value of a property on an object | |
| /// </summary> | |
| /// <param name="propertyInfo">The property on <paramref name="destination"/> to set</param> | |
| /// <param name="destination">The object to modify</param> | |
| /// <param name="value">The value to store</param> | |
| /// <exception cref="InvalidPropertyConversionException">Thrown if <paramref name="value"/> cannot be converted to the type of the property</exception> | |
| public static void SetValue(this PropertyInfo propertyInfo, object destination, object value) | |
| { | |
| if (value == null) | |
| { | |
| return; | |
| } | |
| Type destType = propertyInfo.PropertyType; | |
| if (destType == typeof(bool) && Equals(value, propertyInfo.Name)) | |
| { | |
| value = true; | |
| } | |
| if (propertyInfo.PropertyType.IsAssignableFrom(value.GetType())) | |
| { | |
| propertyInfo.SetValue(destination, value, null); | |
| return; | |
| } | |
| if (propertyInfo.PropertyType.IsNullableOfT()) | |
| { | |
| destType = propertyInfo.PropertyType.GetGenericArguments()[0]; | |
| } | |
| object valueToSet; | |
| try | |
| { | |
| valueToSet = Convert.ChangeType(value, destType); | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new ApplicationException("Could not set property value.", ex); | |
| } | |
| propertyInfo.SetValue(destination, valueToSet, null); | |
| } | |
| public static readonly Regex HtmlStartTagExpression = new Regex(@"<([\w][\w|\d]*)\b[^>]*>"); | |
| public static bool HasHtmlStartTag(this string html) | |
| { | |
| return HtmlStartTagExpression.IsMatch(html); | |
| } | |
| private static readonly Regex emailAddressExpression = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); | |
| public static string ExtractEmailAddress(this string value) | |
| { | |
| var match = emailAddressExpression.Match(value); | |
| if (!match.Success) | |
| { | |
| //_logger.LogWarn("Could not find a RFC 2822 compliant email address in the address {0}. We will continue using the original value.", emailAddressWithDisplayName); | |
| return value; | |
| } | |
| return match.Value; | |
| } | |
| public static string[] ExtractEmailAddresses(this string[] values) | |
| { | |
| var emailAddresses = new List<string>(values.Length); | |
| foreach (var value in values) | |
| { | |
| emailAddresses.Add(value.ExtractEmailAddress()); | |
| } | |
| return emailAddresses.ToArray(); | |
| } | |
| /// <summary> | |
| /// True if the given type is an open or closed Nullable | |
| /// </summary> | |
| /// <param name="theType">The type being tested</param> | |
| /// <returns></returns> | |
| public static bool IsNullableOfT(this Type theType) | |
| { | |
| return theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)); | |
| } | |
| //public static T GetAttribute<T>(this ICustomAttributeProvider provider) where T : Attribute | |
| //{ | |
| // object[] atts = provider.GetCustomAttributes(typeof(T), true); | |
| // return atts.Length > 0 ? atts[0] as T : null; | |
| //} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment