C# 实例化泛型类型时出现InvalidCastException
下面的代码给了我一个C# 实例化泛型类型时出现InvalidCastException,c#,exception,generics,casting,C#,Exception,Generics,Casting,下面的代码给了我一个InvalidCastException,说明我不能在foreach循环中从源类型强制转换到目标类型。我尝试通过该方法传递多个不同的泛型集合,但总是出现此错误。我不明白为什么。任何帮助都将不胜感激 public static void WriteDataListToFile<T>(T dataList, string folderPath, string fileName) where T : IEnumerable, ICollection {
InvalidCastException
,说明我不能在foreach
循环中从源类型强制转换到目标类型。我尝试通过该方法传递多个不同的泛型集合,但总是出现此错误。我不明白为什么。任何帮助都将不胜感激
public static void WriteDataListToFile<T>(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());
}
}
}
public static void writedalitesttofile(T dataList,string folderPath,string fileName),其中T:IEnumerable,ICollection
{
//检查文件是否已经存在
如果(!File.Exists(folderPath+fileName))
{
//如果没有,创建它
创建(folderPath+文件名);
}
使用(StreamWriter sw=新StreamWriter(folderPath+文件名))
{
foreach(数据列表中的T类型)
{
sw.WriteLine(type.ToString());
}
}
}
像这样使用:
foreach (var type in dataList)
{
sw.WriteLine(type.ToString());
}
public static void WriteDataListToFile<T>(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))
{
// added Cast<T>
foreach(T type in dataList.Cast<T>())
{
sw.WriteLine(type.ToString());
}
}
}
您应该尝试使用
Cast
在foreach
中强制转换您的收藏。像这样:
foreach (var type in dataList)
{
sw.WriteLine(type.ToString());
}
public static void WriteDataListToFile<T>(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))
{
// added Cast<T>
foreach(T type in dataList.Cast<T>())
{
sw.WriteLine(type.ToString());
}
}
}
public static void writedalitesttofile(T dataList,string folderPath,string fileName),其中T:IEnumerable,ICollection
{
//检查文件是否已经存在
如果(!File.Exists(folderPath+fileName))
{
//如果没有,创建它
创建(folderPath+文件名);
}
使用(StreamWriter sw=新StreamWriter(folderPath+文件名))
{
//加铸
foreach(在dataList.Cast()中键入T)
{
sw.WriteLine(type.ToString());
}
}
}
您的dataList
应该是一个IEnumerable
public static void writedalitesttofile(IEnumerable数据列表、字符串folderPath、字符串文件名)
{
//检查文件是否已经存在
如果(!File.Exists(folderPath+fileName))
{
//如果没有,创建它
创建(folderPath+文件名);
}
使用(StreamWriter sw=新StreamWriter(folderPath+文件名))
{
foreach(数据列表中的T类型)
{
sw.WriteLine(type.ToString());
}
}
}
您试图将列表中的每个项目键入为T
,但您的类型约束强制T
为IEnumerable
。我认为您希望将参数指定为IEnumerable
,并删除类型约束:
public static void WriteDataListToFile<T>(IEnumerable<T> dataList, string folderPath, string fileName) //no type constraints
{
//your other things
foreach(T type in dataList)
{
sw.WriteLine(type.ToString());
}
}
public static void writedalitesttofile(IEnumerable dataList,string folderPath,string fileName)//无类型约束
{
//你的其他东西
foreach(数据列表中的T类型)
{
sw.WriteLine(type.ToString());
}
}
我感到非常压抑,因为您没有遇到其他异常<代码>文件.Create(folderPath+文件名)错误,您正在打开一个文件流
,但从未关闭它StreamWriter
将创建该文件。如果该文件不存在,请删除最上面的检查代码。处理得很好。应该是显而易见的。谢谢