You can get the list of properties on your object with the following:
PropertyInfo[] properties = result.GetType().GetProperties();
You can then iterate through the list of properties, and create a key/value collection which contains only those that are not null.
var notNullProperties = new Dictionary<string, object>();
foreach (PropertyInfo property in properties)
{
object propertyValue = property.GetValue(result, null);
if (propertyValue != null)
{
notNullProperties.Add(property.Name, propertyValue);
}
}
And then return the serialized dictionary.