title: Check if method is accessing collection
body: <p><h3>Situation:</h3> I am stuck in c# 5.0, I cannot upgrade to c# 6.0 in my current environment until after 2020. Meanwhile the null checks in the codebase have gotten incredibly long, and sometimes properties that are being accessed are not being checked for null before being accessed. A while ago I created a TemporaryLanguageFeatures class that contains some facsimiles of C# 6.0 behavior like nameof. I have decided to create a NullPropagation method that takes an <code>Expression&lt;Func&lt;T&gt;&gt;</code> and checks each for null before moving on to the next object in the series.</p>
  
  <p><h3>Problem:</h3> When the expression includes access to a collection and the collection is empty or the index is out of range of the collection, I get the related empty collection or Index out of range error. I have access to the expression, the LambdaExpression created, and the compiled delegate/its MethodInfo. <b>Edit: I understand that c#6.0 does not protect against these circumstances, however in my method I am still looking to do so.</b></p>
  
  <p><b>Without a try/catch</b> I need to check if the given call would produce an error for being:</p>
  
  <p>A) index out of range (collection has 10 members, expression is as follows): </p>
  
  <pre><code>var answer = NullPropagation(() =&gt; item.Location.PhoneNumbers[500]);
  </code></pre>
  
  <p>B) The collection being empty (collection has no members, expression is as follows): </p>
  
  <pre><code>var answer = NullPropagation(() =&gt; item.Location.PhoneNumbers.First());
  </code></pre>
  
  <h2>Current Code:</h2>
  
  <pre><code>var members = new List&lt;Expression&gt;();
  </code></pre>
  
  <p>...Fill the collection, etc.</p>
  
  <pre><code>var previousMethod = (MethodInfo) null;
  var nullFound = members.Any(x =&gt; 
  {
      if (previousMethod != null &amp;&amp; typeof(IEnumerable).IsAssignableFrom(previousMethod.ReturnType)) 
      {
          Console.WriteLine("HIT!");
          //Todo: Detect collection empty/index out of range, return true;
      }
      previousMethod = method;
      return Expression.Lambda(x).Compile().DynamicInvoke() == null;
  }
  ...
  </code></pre>