title: Concatenate two objects, that I know are of the same IEnumerable<> type
body: <p>Ok, I know that I might have strayed to the realm of overcomplicating things, but I'd still like to know how to make following work. I'm going to rework actual application to don't go to such lengths, but for future reference this seems like a good question.</p>
  
  <p>The situation:</p>
  
  <p>I have an <code>ExpandoObject</code> to which I add properties by <code>string paramName</code>. It all worked great, but then I realized that sometimes I want to have an ExpandoObject property that is some kind of <code>IEnumerable&lt;&gt;</code>, and when I set param for 2nd and subsequent times, I don't want to change the stored list, but I want to concatenate it.</p>
  
  <p>The problem is, of course, that <code>IEnumerable</code> doesn't have <code>Add</code> method. So, I can just replace with a concatenation of two <code>IEnumerable&lt;&gt;</code>'s of the same type. I can sacrifice the ability to use non-generic IEnumerables.</p>
  
  <p>I felt very ingenious until the IntelliSense disallowed me to write <code>eo[paramName] = (eo[paramName] as IEnumerable&lt;&gt;).Concat(param as IEnumerable&lt;&gt;);</code> As you can see from full code, I know for sure, that both <code>param</code> and <code>eo[paramName]</code> are some kind of <code>IEnumerable&lt;&gt;</code>, but I don't know how to tell compiler what I want him to do.</p>
  
  <p>Any suggestions?</p>
  
  <pre><code>private void SetParam&lt;T&gt;(IDictionary&lt;string, object&gt; eo, string paramName, T param){
      // eo is (ExpandoObject as IDictionary&lt;string, object&gt;)
      var enumerableType = GetEnumerableType(typeof (T));
          if (enumerableType == null)
          {
              // is not IEnumerable&lt;&gt;, but it might be not-supported IEnumerable
              if (typeof (T).GetInterfaces().Contains(typeof (System.Collections.IEnumerable)))
                  throw new NotSupportedException("Non-generic collection types are not supported");
  
              // it is just plain not-colection, so set value
              eo[paramName] = param;
          }
          else
          {
              // first ensure that there is at least empty collection
              if (eo[paramName] == null)
                  eo[paramName] = Activator.CreateInstance&lt;T&gt;();
  
              // and concatenate
              eo[paramName] = (eo[paramName] as IEnumerable&lt;&gt;).Concat(param as IEnumerable&lt;&gt;);
             // or whatever other way to add param to eo[paramName]
          }
      } 
      // from: http://stackoverflow.com/a/1846690/1417905
      private static Type GetEnumerableType(Type type)
      {
          return (from intType in type.GetInterfaces()
              where intType.IsGenericType &amp;&amp; intType.GetGenericTypeDefinition() == typeof (IEnumerable&lt;&gt;)
              select intType.GetGenericArguments()[0]).FirstOrDefault();
      }
  </code></pre>