Find all collection properties of an object and get their values I have a chunk of code that get's all of the ICollection properties of the passed in object and then takes all of their values and adds them to another ICollection. 


  End goal is to see which properties have child objects in them and
  thus finding out how many dependents the object has.


The object is an EntityFramework entity so the children would be other entities. I'm not entirely convinced that I'm actually grabbing the correct properties (although in my tests so far it works) and I'm not convinced I'm parsing them efficiently either. Here's the code, and I welcome any recommendations:

public class DependencyValidator&lt;TEntity&gt; :
    IDependencyValidator&lt;TEntity&gt;
    where TEntity : class {
    private readonly Type IEnumerableType = typeof(IEnumerable);
    private readonly Type StringType = typeof(String);

    public IEnumerable&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; Dependencies(
        TEntity entity) {
        if (entity == null) {
            return Enumerable.Empty&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();
        }

        ICollection&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; dependents = new List&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();

        entity.GetType().GetProperties().Where(
            p =&gt;
                this.IEnumerableType.IsAssignableFrom(p.PropertyType)
                &amp;&amp; !this.StringType.IsAssignableFrom(p.PropertyType)).ForEach(
            p =&gt; {
                IEnumerable values = (p.GetValue(entity) as IEnumerable);
                ICollection&lt;string&gt; children = new List&lt;string&gt;();

                foreach (var value in values) {
                    children.Add(value.ToString());
                }

                dependents.Add(new KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;(p.Name, children));
            });

        return dependents;
    }
}
First up, let's clean up the formatting a bit.

public class DependencyValidator&lt;TEntity&gt; :
    IDependencyValidator&lt;TEntity&gt;
    where TEntity : class
{
    private readonly Type IEnumerableType = typeof(IEnumerable);
    private readonly Type StringType = typeof(String);

    public IEnumerable&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; Dependencies(TEntity entity)
    {
        if (entity == null)
        {
            return Enumerable.Empty&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();
        }

        ICollection&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; dependents = new List&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();

        entity.GetType().GetProperties().Where(
            p =&gt;
                this.IEnumerableType.IsAssignableFrom(p.PropertyType)
                &amp;&amp; !this.StringType.IsAssignableFrom(p.PropertyType)).ForEach(
            p =&gt;
            {
                IEnumerable values = (p.GetValue(entity) as IEnumerable);
                ICollection&lt;string&gt; children = new List&lt;string&gt;();

                foreach (var value in values)
                {
                    children.Add(value.ToString());
                }

                dependents.Add(new KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;(p.Name, children));
            });

        return dependents;
    }
}


Now let's use the var keyword to make it less verbose.

public class DependencyValidator&lt;TEntity&gt; :
    IDependencyValidator&lt;TEntity&gt;
    where TEntity : class
{
    private readonly Type IEnumerableType = typeof(IEnumerable);
    private readonly Type StringType = typeof(String);

    public IEnumerable&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; Dependencies(TEntity entity)
    {
        if (entity == null)
        {
            return Enumerable.Empty&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();
        }

        var dependents = new List&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();

        entity.GetType().GetProperties().Where(
            p =&gt;
                this.IEnumerableType.IsAssignableFrom(p.PropertyType)
                &amp;&amp; !this.StringType.IsAssignableFrom(p.PropertyType)).ForEach(
            p =&gt;
            {
                var values = (p.GetValue(entity) as IEnumerable);
                var children = new List&lt;string&gt;();

                foreach (var value in values)
                {
                    children.Add(value.ToString());
                }

                dependents.Add(new KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;(p.Name, children));
            });

        return dependents;
    }
}


Your ForEach method seems to be non-standard, so I'd like to get rid of that.

public class DependencyValidator&lt;TEntity&gt; :
    IDependencyValidator&lt;TEntity&gt;
    where TEntity : class
{
    private readonly Type IEnumerableType = typeof(IEnumerable);
    private readonly Type StringType = typeof(String);

    public IEnumerable&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; Dependencies(TEntity entity)
    {
        if (entity == null)
        {
            return Enumerable.Empty&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();
        }

        var dependents = new List&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();

        var properties = entity.GetType()
            .GetProperties()
            .Where(p =&gt; this.IEnumerableType.IsAssignableFrom(p.PropertyType) &amp;&amp; !this.StringType.IsAssignableFrom(p.PropertyType));

        foreach (var property in properties)
        {
            var values = property.GetValue(entity) as IEnumerable;
            var children = new List&lt;string&gt;();

            foreach (var value in values)
            {
                children.Add(value.ToString());
            }

            dependents.Add(new KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;(property.Name, children));
        }

        return dependents;
    }
}


ReSharper suggests replacing the inner loop with a LINQ expression, which I think looks alright.

public class DependencyValidator&lt;TEntity&gt; :
    IDependencyValidator&lt;TEntity&gt;
    where TEntity : class
{
    private readonly Type IEnumerableType = typeof(IEnumerable);
    private readonly Type StringType = typeof(String);

    public IEnumerable&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; Dependencies(TEntity entity)
    {
        if (entity == null)
        {
            return Enumerable.Empty&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();
        }

        var dependents = new List&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();

        var properties = entity.GetType()
            .GetProperties()
            .Where(p =&gt; this.IEnumerableType.IsAssignableFrom(p.PropertyType) &amp;&amp; !this.StringType.IsAssignableFrom(p.PropertyType));

        foreach (var property in properties)
        {
            var values = property.GetValue(entity) as IEnumerable;
            var children = (from object value in values select value.ToString()).ToList();

            dependents.Add(new KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;(property.Name, children));
        }

        return dependents;
    }
}


Finally, we can make the fields static. ReSharper warns about this, but I think it's fine in this case.

 public class DependencyValidator&lt;TEntity&gt; :
    IDependencyValidator&lt;TEntity&gt;
    where TEntity : class
{
    private static readonly Type IEnumerableType = typeof(IEnumerable);
    private static readonly Type StringType = typeof(string);

    public IEnumerable&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt; Dependencies(TEntity entity)
    {
        if (entity == null)
        {
            return Enumerable.Empty&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();
        }

        var dependents = new List&lt;KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;&gt;();

        var properties = entity.GetType()
            .GetProperties()
            .Where(p =&gt; IEnumerableType.IsAssignableFrom(p.PropertyType) &amp;&amp; !StringType.IsAssignableFrom(p.PropertyType));

        foreach (var property in properties)
        {
            var values = property.GetValue(entity) as IEnumerable;
            var children = (from object value in values select value.ToString()).ToList();

            dependents.Add(new KeyValuePair&lt;string, IEnumerable&lt;string&gt;&gt;(property.Name, children));
        }

        return dependents;
    }
}