Skip to content

Instantly share code, notes, and snippets.

@alex-berezan
Created October 25, 2013 19:21
Show Gist options
  • Save alex-berezan/7160350 to your computer and use it in GitHub Desktop.
Save alex-berezan/7160350 to your computer and use it in GitHub Desktop.
Fills specified instance with default values.
private static void FillInstanceWithDefaults(object instance)
{
if (instance == null)
return;
if (instance.GetType().FullName.StartsWith("System."))
return;
instance.GetType()
.GetProperties()
.ToList()
.ForEach(property =>
{
if (property.PropertyType == typeof(string))
{
property.SetValue(instance, "string value");
}
else if (!property.PropertyType.IsValueType)
{
var value = Activator.CreateInstance(property.PropertyType);
FillInstanceWithDefaults(value);
property.SetValue(instance, value);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment