Skip to content

Instantly share code, notes, and snippets.

@CodyEngel
Created August 4, 2014 18:02
Show Gist options
  • Save CodyEngel/da3c692d734a7700cd83 to your computer and use it in GitHub Desktop.
Save CodyEngel/da3c692d734a7700cd83 to your computer and use it in GitHub Desktop.
Return All Non-Null Parameters As Formatted String
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