Vnext 参数 1:无法从“字符串”转换为“System.IO.Stream”
本文关键字:字符串 转换 System IO Stream 参数 Vnext | 更新日期: 2023-09-27 18:36:04
我正在尝试使用 Vnext 项目创建一个通用序列化程序,当我调用 StreamWriter 的构造函数时,它会抛出此编译器错误
错误 CS1503 参数 1: 无法从"字符串"转换为 "System.IO.Stream" Test.ASP.NET Core 5.0 助手.cs 14
即使有一个构造函数允许将文件的路径指定为参数。
这是我的类文件
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace Test
{
public static class Helper
{
public static void SerializeToXml<T>(string path, T value)
{
var serializer = new XmlSerializer(typeof(T));
using (var stream = new StreamWriter(path)) // ERROR OCCURS HERE
{
using (var writer = XmlWriter.Create(stream))
{
serializer.Serialize(writer, value);
}
}
}
}
}
这是我的项目.json文件
{
"version": "1.0.0-*",
"dependencies": {
},
"commands": {
"run": "run"
},
"frameworks": {
"aspnet50": {
"dependencies": {
},
"frameworkAssemblies": {
"System.Xml": "4.0.0.0"
}
},
"aspnetcore50": {
"dependencies": {
"System.Console": "4.0.0-beta-22231",
"System.Xml.XmlSerializer": "4.0.0-beta-22231",
"System.Collections": "4.0.10-beta-22422",
"System.Xml.ReaderWriter": "4.0.10-beta-22231",
"System.IO": "4.0.10-beta-22231"
}
}
}
}
这是大卫家禽的答案
那是因为它在 CoreCLR 上不可用。使用新的 StringWriter(File.OpenWrite(path))
为了将来参考,我可以在哪里检查某个功能是否可用?
https://github.com/dotnet/corefx 存储库上的文件问题。他们 将能够澄清为什么新框架中缺少某些内容。我 相信删除此特定过载的原因是因为 新包之间的分层问题。
包含 StreamWriter 的程序集不应直接 引用文件流:
new StreamReader(path)
实际上确实如此
new StreamReader(new FileStream(path, options)).