Created
October 27, 2011 09:10
-
-
Save Grinderofl/1319117 to your computer and use it in GitHub Desktop.
Recursion Limiter
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
public static object Limit(object o, int level) | |
{ | |
if (level <= 0) | |
return null; | |
PropertyInfo[] properties = o.GetType().GetProperties(); | |
foreach (var property in properties) | |
{ | |
if (property.PropertyType.IsClass && property.CanWrite && property.PropertyType is IEnumerable) | |
{ | |
property.SetValue(o, Limit(Convert.ChangeType(property, property.PropertyType), level - 1), null); | |
} | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment