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?
You probably want
DataSourceto be the non-genericIEnumerable. AnyIEnumerable<T>is also anIEnumerable, 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 typeIEnumerable<Animal>. However, if you want to allow any type in the data source, you’d still need to make DataSource anIEnumerable<object>. You can’t do anything with anIEnumerable<object>that you can’t do with anIEnumerable.