0

I'm using Analyzer with Code Fix template of .NET Compiler Platform SDK to create my Roslyn code analyzer. The template creates a solution, where the actual analyzer project is a multi-target one. (I did not find a way, to modify this.)

Now I want to do something simple: accessing a file containing some setup data while initializing my analyzer.

Actually, it turns out, that I cannot create a TextReader because .net 4x and .net core are using different kinds of namespaces, System.IO.File for example, is in a different assembly in the two above cases. I should install a NuGet package (System.IO.FileSystem) to access System.IO.File, but I can not do this, because the given package is not multi-target.

Does not work:

using (TextReader reader = new File.OpenText(@"filepath"))
{
    ....
}

... because File needs NuGet package, which I can not install.

Does not work either:

using (TextReader reader = new StreamReader(@"filepath"))
{
    ....
}

... because StreamReader does not have the signature with a single string parameter, only the stream based ones are accessible.

Now I'm confused. How do I use TextReader in such a multi-target project?

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
Arpicheck
  • 1
  • 1
  • System.IO.File is very-much multi-target, and if you're targeting .NET Standard (especially lower levels), it is quite common to have to add extra dependencies; the types being in different *assemblies* shouldn't matter at all - the packaging deals with that quite effectively. You say "which I can not install" - why not? what happens? What TFMs are you targeting currently? – Marc Gravell Jul 13 '18 at 08:16
  • Trying to add the given packages comes back with the following error message: Could not install package 'System.IO.FileSystem 4.3.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile7', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. I try to add the package to my multi-target project. – Arpicheck Jul 13 '18 at 08:18
  • hmmm; k; so - can you get hold of the *stream* in all cases? either `File.OpenRead(...)` or `new FileStream(...)`? if you can get the `Stream`, you should be able to pass *that* into the constructor of `StreamReader`? – Marc Gravell Jul 13 '18 at 08:22
  • But I can not. As I mentioned, I can't add the package, therefore I can not access File at all, nor FileStream, and Stream itself is abstract. – Arpicheck Jul 13 '18 at 08:23

0 Answers0