Quantcast
Channel: Questions in topic: "exception"
Viewing all articles
Browse latest Browse all 470

InvalidCastException when instancing generic type

$
0
0
The following code gives me an "InvalidCastException", stating that I can't cast from source to destination type in the foreach loop. I've tried passing multiple different generic collections through the method and I always get this error. I can't figure out why. Any help would be appreciated. public static void WriteDataListToFile(T dataList, string folderPath, string fileName) where T : IEnumerable, ICollection { //Check to see if file already exists if(!File.Exists(folderPath + fileName)) { //if not, create it File.Create(folderPath + fileName); } using(StreamWriter sw = new StreamWriter(folderPath + fileName)) { foreach(T type in dataList) { sw.WriteLine(type.ToString()); } } } Thanks, Matt

Viewing all articles
Browse latest Browse all 470

Trending Articles