Skip to content

Instantly share code, notes, and snippets.

@chris-muller
Created February 21, 2016 03:37
Show Gist options
  • Save chris-muller/51ed3851f580f7455d6b to your computer and use it in GitHub Desktop.
Save chris-muller/51ed3851f580f7455d6b to your computer and use it in GitHub Desktop.
Object Extension to trim all string properties.
//http://stackoverflow.com/a/15328864
public static class StringExtensions
{
public static TSelf TrimStringProperties<TSelf>(this TSelf input)
{
var stringProperties = input.GetType().GetProperties()
.Where(p => p.PropertyType == typeof(string));
foreach (var stringProperty in stringProperties)
{
string currentValue = (string)stringProperty.GetValue(input, null);
if (currentValue != null)
stringProperty.SetValue(input, currentValue.Trim(), null);
}
return input;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment