Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert an array to IEnumerable

Tags:

c#

When I hit this line of code

(T)Convert.ChangeType(data.Split(new char[] { '¤' }), typeof(T));

I get an error of Object doesn't implement IConvertible. T is an IEnumerable<String> and data is String typed. What can be done about it?

I've tried converting it to a bunch of different stuff explicitly before the change of type but I can't get it to work. I'm converting other stuff to Dictionary and native types but this particular one gives me headache.

like image 945
Konrad Viltersten Avatar asked Dec 11 '22 09:12

Konrad Viltersten


2 Answers

Why do you need to call Convert.ChangeType()?

In the following code, theEnumerable is of type IEnumerable<string>.

var inputString = "a,b,c";
var theEnumerable = inputString.Split(',').AsEnumerable();
like image 167
Steve Avatar answered Jan 02 '23 21:01

Steve


Fundementally the type of an object cannot be an Interface, only a type that implements one. Since Array implements IEnumberable your string[] already is what you want it to be.

like image 41
Ashigore Avatar answered Jan 02 '23 20:01

Ashigore