title: IList to EntityCollection with reflection
body: <p>In an application that I write, I build the interface via reflection from the data context. Displaying the values is no problem, but creating the item collections and assigning values via reflection does not work.</p>
  
  <p>Here is the code in question:</p>
  
  <pre><code>var listItemType = property.PropertyType.GetGenericArguments().First();
  // See remark #1
  var listType = typeof(List&lt;&gt;).MakeGenericType(new[] { listItemType });
  var assocItems = Activator.CreateInstance(listType) as IList;
  var listSelector = EditorPanel.FindControl(property.Name) as PropertyListBox;
  if (listSelector != null)
  {
      foreach (var selectedItem in listSelector.SelectedItems)
      {
          assocItems.Add(selectedListItem);
      }
  }
  // See remark #2
  property.SetValue(itemToUpdate, assocItems, null);
  </code></pre>
  
  <p><strong>Remark #1:</strong></p>
  
  <p>I tried to change the line to <code>var listType = typeof(EntityCollection&lt;&gt;).MakeGenericType(new[] {listItemType});</code> and then cast <code>assocItems</code> to IListSource. Instead of <code>assocItems.Add()</code> I called <code>assocItems.GetList().Add()</code>, but that resulted in an <code>InvalidOperationException</code>:</p>
  
  <blockquote>
    <p>The object could not be added to the
    EntityCollection or EntityReference.
    An object that is attached to an
    ObjectContext cannot be added to an
    EntityCollection or EntityReference
    that is not associated with a source
    object. </p>
  </blockquote>
  
  <p><strong>Remark #2:</strong></p>
  
  <p>Here I need to convert the <code>IList</code> to an <code>EntityCollection&lt;T&gt;</code> somehow.</p>