Created
February 21, 2016 03:37
-
-
Save chris-muller/51ed3851f580f7455d6b to your computer and use it in GitHub Desktop.
Object Extension to trim all string properties.
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
//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