Created
August 4, 2014 18:02
-
-
Save CodyEngel/da3c692d734a7700cd83 to your computer and use it in GitHub Desktop.
Return All Non-Null Parameters As Formatted String
This file contains 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
private String getParameters<T>(T obj) | |
{ | |
StringBuilder parameters = new StringBuilder(); | |
var properties = typeof(T).GetProperties(); | |
foreach(Property property in properties) | |
{ | |
if(property.GetValue(obj, null) != null) | |
{ | |
String propertyValue = property.GetValue(obj, null).ToString(); | |
String propertyName = property.Name.ToString(); | |
if(!string.IsNullOrWhiteSpace(propertyValue)) | |
{ | |
if(parameters.Length != 0){ | |
parameters.AppendLine(); | |
} | |
parameters.Append(propertyName) | |
.Append(" = ") | |
.Append(propertyValue) | |
} // if propertyValue is not null or empty | |
} // if property.GetValue is not null | |
} // foreach property in properties | |
return parameters.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment