Vnext参数1:不能从“字符串”转换为“的System.IO.Stream” [英] Vnext Argument 1: cannot convert from 'string' to 'System.IO.Stream'

查看:1081
本文介绍了Vnext参数1:不能从“字符串”转换为“的System.IO.Stream”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个通用串行withing一个Vnext项目并当我打电话的构造StreamWriter的它抛出这个编译器错误




错误CS1503参数1:不能从字符串转换为
'的System.IO.Stream'Test.ASP.NET核心5.0 Helper.cs 14


< /块引用>

即使有一个构造函数,允许指定一个文件作为参数的路径。



这是我的类文件

 使用系统; 
使用System.Collections.Generic;
使用的System.Xml;
使用的System.Xml.Serialization;
:使用System.IO;

命名空间测试
{
公共静态类助手
{
公共静态无效SerializeToXml< T>(字符串路径,T值)
{
无功序列化=新的XmlSerializer(typeof运算(T));错误发生时这里
使用(VAR流=新的StreamWriter(路径))//使用
{
(VAR作家= XmlWriter.Create(流))
{
serializer.Serialize(作家,价值);
}
}
}
}
}

这是我的project.json文件

  {
版本:1.0.0- * ,
依赖:{
},
命令:{
运行:在运行
},
框架 :{
aspnet50:{
依赖:{

},
frameworkAssemblies:{
的System.Xml: 4.0.0.0

}
},
aspnetcore50:{
依赖:{
System.Console:4.0。 0-β-22231,
System.Xml.XmlSerializer:4.0.0-β-22231,
System.Collections中:4.0.10-β-22422,
System.Xml.ReaderWriter:4.0.10-β-22231,
System.IO:4.0.10-β-22231
}
}
}
}


解决方案

下面从 davidfowl




那是因为它的答案不提供CoreCLR。使用新的
的StringWriter(File.OpenWrite(路径)),而不是




有关将来参考我在哪里可以检查一个功能可用?




文件在 https问题:// github上。 COM / DOTNET / corefx 库。他们
就能解释,为什么东西,在新的框架缺失。我
相信辗转这个特殊超载的原因是因为新的软件包之间
分层的问题。



这包含的StreamWriter大会应不直接
引用的FileStream:




 新的StreamReader(路径)

实际执行

 新的StreamReader(新的FileStream(路径选择))。 


I am trying to create a generic serializer withing a Vnext project and when I call the constructor for StreamWriter it throws this compiler error

Error CS1503 Argument 1: cannot convert from 'string' to 'System.IO.Stream' Test.ASP.NET Core 5.0 Helper.cs 14

even though there is a constructor that allows to specify a path to a file as an argument.

this is my class file

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);
                }
            }
        }
    }
}

This is my project.json file

{
    "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"
            }
        }
    }
}

解决方案

Here is the Answer from davidfowl

Thats because it's not available on CoreCLR. Use new StringWriter(File.OpenWrite(path)) instead

For future reference where can I check if a function is available or not?

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)

actually does

new StreamReader(new FileStream(path, options)).

这篇关于Vnext参数1:不能从“字符串”转换为“的System.IO.Stream”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆