Vnext 参数 1:无法从 'string' 转换为 'System.IO.Stream'
Vnext Argument 1: cannot convert from 'string' to 'System.IO.Stream'
我正在尝试使用 Vnext 项目创建一个 通用序列化器 ,当我调用 StreamWriter 的构造函数时,它会抛出此编译器错误
Error CS1503 Argument 1: cannot convert from 'string' to
'System.IO.Stream' Test.ASP.NET Core 5.0 Helper.cs 14
尽管有一个构造函数允许将文件路径指定为参数。
这是我的 class 文件
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);
}
}
}
}
}
这是我的 project.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"
}
}
}
}
这是来自davidfowl
的回答
Thats because it's not available on CoreCLR. Use new
StringWriter(File.OpenWrite(path)) instead
为了将来参考,我在哪里可以检查功能是否可用?
File issues on the https://github.com/dotnet/corefx repository. They
will be able to clarify why things are missing in the new framework. I
believe the reason this particular overload was removed was because of
layering issues between the new packages.
The assembly that contains StreamWriter shouldn't be directly
referencing FileStream:
new StreamReader(path)
实际上
new StreamReader(new FileStream(path, options)).
我正在尝试使用 Vnext 项目创建一个 通用序列化器 ,当我调用 StreamWriter 的构造函数时,它会抛出此编译器错误
Error CS1503 Argument 1: cannot convert from 'string' to 'System.IO.Stream' Test.ASP.NET Core 5.0 Helper.cs 14
尽管有一个构造函数允许将文件路径指定为参数。
这是我的 class 文件
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);
}
}
}
}
}
这是我的 project.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"
}
}
}
}
这是来自davidfowl
的回答Thats because it's not available on CoreCLR. Use new StringWriter(File.OpenWrite(path)) instead
为了将来参考,我在哪里可以检查功能是否可用?
File issues on the https://github.com/dotnet/corefx repository. They will be able to clarify why things are missing in the new framework. I believe the reason this particular overload was removed was because of layering issues between the new packages.
The assembly that contains StreamWriter shouldn't be directly referencing FileStream:
new StreamReader(path)
实际上
new StreamReader(new FileStream(path, options)).