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)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With