gpt4 book ai didi

c# - Vnext 参数 1 : cannot convert from 'string' to 'System.IO.Stream'

转载 作者:太空狗 更新时间:2023-10-29 20:09:53 25 4
gpt4 key购买 nike

我正在尝试使用 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)).

关于c# - Vnext 参数 1 : cannot convert from 'string' to 'System.IO.Stream' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27735447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com