/// <summary>
/// This does NOT go into sub objects, only the top level object
/// i.e. if you have a class with a string field, it will trim that string field value
/// but not trim any string fields on sub-objects inside the containing class
/// </summary>
/// <param name="currentObject"></param>
public static void SafeTrimAllStringFields(this object currentObject)
{
if (currentObject == null)
{
return;
}
var type = currentObject.GetType();
var stringFields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(f => f.FieldType == typeof(string));
foreach (var field in stringFields)
{
var value = (string)field.GetValue(currentObject);
if (value != null)
{
field.SetValue(currentObject, value.SafeTrim());
}
}
}
}
public static string SafeTrim(this string value)
{
return value == null ? string.Empty : value.Trim();
}
Category Archives: Extension Methods
Extension Method Bliss
In the last six months, I have dabbled a little bit in Ruby. What I found was that I didn’t really want to work in Ruby but I wanted some of the beautiful syntax. I use extension methods to bring some of that goodness back to C#.
Here are a few favorites:
public static bool IsANumber(this string value) { float output; return float.TryParse(value, out output); } public static bool IsNotANumber(this string value) { float output; return !float.TryParse(value, out output); } public static string FormattedAsAPhoneNumberWithDashes(this string s) { if(s.IsNotANumber() || s.Length != 10) { return string.Empty; } StringBuilder formattedPhone = new StringBuilder(); formattedPhone.Append(s.Substring(0, 3)); formattedPhone.Append("-"); formattedPhone.Append(s.Substring(3, 3)); formattedPhone.Append("-"); formattedPhone.Append(s.Substring(6, 4)); return formattedPhone.ToString(); } public static string XmlEncode(this string s) { return string.IsNullOrWhiteSpace(s) ? string.Empty : System.Security.SecurityElement.Escape(s); } public static bool IsNullOrWhiteSpace(this string value) { return string.IsNullOrWhiteSpace(value); } public static string SafeTrim(this string value) { return value == null ? string.Empty : value.Trim(); } public static string SafeParseStringToDate(this string dateToParse) { DateTime dtTemp; return DateTime.TryParse(dateToParse, out dtTemp) ? dtTemp.ToString() : string.Empty; } public static string TrimmedAndLowered(this string value) { return value.SafeTrim().ToLower(); } public static string AddAProperCommaToAName(this string lastname, string firstname) { if (!string.IsNullOrWhiteSpace(lastname) && !string.IsNullOrWhiteSpace(firstname)) return lastname + ", " + firstname; return !string.IsNullOrWhiteSpace(lastname) ? lastname : firstname; } public static bool IsNotNull(this object obj) { return obj != null; } public static bool IsNull(this object obj) { return obj == null; } public static bool In(this string value, params string[] paramArray) { return value.IsNotNull() && paramArray.Any(param => param.TrimmedAndLowered() == value.TrimmedAndLowered()); } public static bool In(this int? value, params int[] paramArray) { return value.IsNotNull() && paramArray.Any(param => param == value); } public static bool In(this int value, params int[] paramArray) { return paramArray.Any(param => param == value); } public static string RemoveNumericCharacters(this string value) { Regex regex = new Regex("[0-9]"); value = regex.Replace(value, ""); return value; } public static string RemoveNonNumericCharacters(this string value) { if (value == null) return string.Empty; Regex onlyNumeric = new Regex(@"[^\d]"); var result = onlyNumeric.Replace(value, string.Empty); return result; }