0

I'm trying to compile a class from text at runtime. My problem is that my class uses a valueTupe in a function (AllLines), and I receive an error "C:\xxxx.cs(19,28): error CS0570: 'BaseClass.AllLines' is not supported by the language" when using this code

CodeDomProvider objCodeCompiler = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();

CompilerParameters objCompilerParameters = new CompilerParameters();

objCompilerParameters.ReferencedAssemblies.Add("mscorlib.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.IO.dll");
objCompilerParameters.ReferencedAssemblies.Add("System.Linq.dll");
CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile(objCompilerParameters, filename);

EDIT:

The Textfile looks as follows :

using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
    public abstract class BaseClass
    {
        public List<(int LineNumber, string Value)> AllLines
        {
            ...
        }
    }
}

I'm using Microsoft.CodeDom.Providers.DotNetCompilerPlatform v2.0.0.0, Microsoft (R) Visual C# Compiler version 1.0.0.50618

Unsure if that is the actual version of roslyn.

George Alexandria
  • 2,841
  • 2
  • 16
  • 24
WynDiesel
  • 1,104
  • 7
  • 38
  • 1
    Could you provide a [mcve], including the package versions you're using? (In particular, if you're using an old version of Roslyn, that would definitely explain it.) – Jon Skeet Jun 28 '18 at 13:28
  • Thanks. Done so, please see my edits. Unsure if that is my roslyn version. – WynDiesel Jun 28 '18 at 13:44
  • Trying just the code there gives me an entirely different error. This is why I was asking for a complete example - something we can build and run really easily. – Jon Skeet Jun 28 '18 at 13:53
  • @george-alexandria : I see that in your review/edit, you removed the roslyn tag. Am I not using roslyn to compile the code, or what was the reason for suggesting the edit? – WynDiesel Jun 29 '18 at 06:41
  • @DaisyShipton. So, your suggestion is inadvertently guiding me to the solution, I think. While making a small example to demonstrate my problem, using the same logic/idea, in the same project, I'm -not- getting the error, even though I'm still returning a named tuple. Thank you very much. Even though this doesn't answer my question, I can now start adding things and testing with a working example. – WynDiesel Jun 29 '18 at 06:47
  • 1
    @WynDiesel, you directly use `CodeDom`, not `Roslyn`. – George Alexandria Jun 29 '18 at 07:05
  • 1
    @WynDiesel: It's not really inadvertent - the process of coming up with a complete example *very* often solves the problem :) – Jon Skeet Jun 29 '18 at 07:42
  • Thanks @GeorgeAlexandria, I was under the impression codeDom is roslyn. – WynDiesel Jun 29 '18 at 08:00

1 Answers1

0

Firstly, you were correct that you were using Roslyn as you are using Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider from the NuGet package Microsoft.CodeDom.Providers.DotNetCompilerPlatform.

However, the issue you are facing is that your text file does not contain valid C#.

  1. Your declaration of List<T> is invalid in enclosing the type parameters in parentheses
  2. You are adding names(?) to the type parameter declaration (LineNumber, Value).
  3. You're providing two type parameters when List<T> only accepts one. (Maybe you meant to use a Dictionary<TKey, TValue>)
  4. There is no body to your property declaration

Try replacing your text file with:

using System;
using System.Collections.Generic;
using System.Linq;
namespace MyNamespace
{
    public abstract class BaseClass
    {
        public Dictionary<int, string> AllLines
        {
            get; set;
        }
    }
}

Note, you don't actually need using System or using System.Linq for this example. Also note, you don't need to use Roslyn for this. The old fashioned CodeDOM can compile it (replace Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider with Microsoft.CSharp.CSharpCodeProvider).

Andy Jones
  • 584
  • 2
  • 14
  • Thanks for your reply, Andy. On points 1,2 and 3, C# 7 has a new feature called named tuples, which is used as per my example (see https://learn.microsoft.com/en-us/dotnet/csharp/tuples). On point 4, you are correct, in my example i didnt supply code for the property. This wasn't my problem either. The file's code complies fine, there is not problem with the actual code. I still haven't figured out what the problem was, I ended up using CodeDom, as per your suggestion. In lack of a better answer, I am going to accept yours. – WynDiesel Jul 05 '18 at 12:11