tags:

views:

24

answers:

1

I'm trying to create a user control which has a DataSource, which can accept any form of collection, such as List<string> or string[]

If I check for is IEnumerable then it requires a type, checking is IEnumerable<Type> returns false, as does is IEnumerable<object>

What's the correct way to go about this? I presume I set the DataSource property to object and then check on the setter for the correct type?

A: 

You probably want DataSource to be the non-generic IEnumerable. Any IEnumerable<T> is also an IEnumerable, which allows you to enumerate the collection without the strong typing that the generic interface gives you.

.NET 4.0 introduces covariance, so you can assign a IEnumerable<Dog> to a variable of type IEnumerable<Animal>. However, if you want to allow any type in the data source, you'd still need to make DataSource an IEnumerable<object>. You can't do anything with an IEnumerable<object> that you can't do with an IEnumerable.

stevemegson