Created
October 25, 2013 19:21
-
-
Save alex-berezan/7160350 to your computer and use it in GitHub Desktop.
Fills specified instance with default values.
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 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