I have an object that is contains strings and further objects that contain strings, what i need to do is ensure that the object and any sub objects have an empty string and not
You can modify your current code to get all sub objects and then perform the same check for null string properties.
public void SetNullPropertiesToEmptyString(object root) {
var queue = new Queue<object>();
queue.Enqueue(root);
while (queue.Count > 0) {
var current = queue.Dequeue();
foreach (var property in current.GetType().GetProperties()) {
var propertyType = property.PropertyType;
var value = property.GetValue(current, null);
if (propertyType == typeof(string) && value == null) {
property.SetValue(current, string.Empty);
} else if (propertyType.IsClass && value != null && value != current && !queue.Contains(value)) {
queue.Enqueue(value);
}
}
}
}