I’ve written this method to cast a comma separated string into a List of its type:
public List<T> GetListFromString<T>(string commaSplited)
{
return commaSplited.Split(',').Cast<T>().ToList();
}
But it throws an exception saying ‘The specified cast is not valid.’
I’ve tested it with long input.
Your code certainly works if T is string (I tested it).
If T is something else, say int, you will get this Exception.
This Works
This Fails
That is because one cannot cast or convert string to int, e.g. the following would fail too:
The Fix
However all of the given strings must be convertable to T, e.g. the following would still fail:
because “abc” cannot be converted to type int.
Also, T must be some type that System.Convert() knows how to convert to from a string.