0

I have a 3rd party library I am working with and have no access to change it. It has a property on one of its components that only accepts a ThirdPartyList<T>(IList<T> items), but at the point I am about to set the property I have only an IEnumerable.

I can figure out what the type is going to be in my IEnumerable by looking at the first item and figuring it out with reflection and all the items in the IEnumerable will be of the same type, but the question is how do I turn that type into a new ThirdPartyList<what do I put here>().

Thanks

CD

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • Also check out [this](http://stackoverflow.com/questions/18989054/instantiate-a-generic-class-using-reflection?lq=1) or [this](http://stackoverflow.com/questions/15661382/how-to-instantiate-a-generic-list-out-of-an-unkown-type?lq=1) or [this](http://stackoverflow.com/questions/18885453/creating-an-instance-of-generic-class-using-a-type-reflected-from-an-assembly?lq=1) or [this](http://stackoverflow.com/questions/1154874/c-sharp-instantiate-a-list-given-a-reference-to-a-type?lq=1). – O. R. Mapper May 20 '14 at 10:07
  • 1
    @AnthonyLambert You still have to convince the compiler that you do so in a way that's consistent (most importantly, type-compatible) with every other use of the generic structure. In fact, *that* is the point of generics; you can put everything in a `System.Collections.ArrayList` by casting it to `Object`. –  May 20 '14 at 10:08
  • @AnthonyLambert: The question is about *how* to put something particular *that the OP wants* there (in this case, the item type of the enumerable). – O. R. Mapper May 20 '14 at 10:09

1 Answers1

-1

Your element type , for example

IEnumerable<Product> products = GetProductList(); //Your code

ThirdPartyList<Product> customList = new ThirdPartyList<Product>(products);
Toan Nguyen
  • 11,263
  • 5
  • 43
  • 59
  • 1
    The issue appears to be that `Product` is not known at the time the code is written. –  May 20 '14 at 10:09