Skip to content

Instantly share code, notes, and snippets.

@Grinderofl
Created October 27, 2011 09:10
Show Gist options
  • Save Grinderofl/1319117 to your computer and use it in GitHub Desktop.
Save Grinderofl/1319117 to your computer and use it in GitHub Desktop.
Recursion Limiter
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