Useful or not, from you.
cs-script CsvHelper loads the wrong ValueTuple version in CSScript?

I use the CsvHelper to read some csv files, when I run my script on a computer with dotnet 4.6 is the highest dotnet framework version, something weird happens, this is my code:

//css_args -provider:%CSSCRIPT_DIR%\lib\CSSRoslynProvider.dll
//css_nuget CsvHelper
using CsvHelper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Win32;

public class GameScriptBuilder
{
   static void Main(string[] args)
   {
      Console.WriteLine($".NET Runtime Version: {Environment.Version}");
      Get45PlusFromRegistry();
      var CsvHelperName = Assembly.GetExecutingAssembly().GetReferencedAssemblies()[1];
      Console.WriteLine($"CsvHelper fullname: \n   {CsvHelperName}");
      var ValueTupleName = Assembly.Load(CsvHelperName).GetReferencedAssemblies()[4];
      Console.WriteLine($"ValueTuple fullname: \n   {ValueTupleName}");
      var ValueTupleLocation = Assembly.Load(ValueTupleName).Location;
      Console.WriteLine($"ValueTuple location: \n   {ValueTupleLocation}");
      Console.WriteLine("Try to use CsvHelper library, in which has System.ValueTuple dependency.");
      Console.WriteLine(new CsvReader((TextReader)null));
   }
   // https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
   static void Get45PlusFromRegistry()
   {
      const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
      using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
      {
         if (ndpKey != null && ndpKey.GetValue("Release") != null) {
            Console.WriteLine($".NET Framework Version: {CheckFor45PlusVersion((int) ndpKey.GetValue("Release"))}");
         }
         else {
            Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
         } 
      }   
      // Checking the version using >= enables forward compatibility.
      string CheckFor45PlusVersion(int releaseKey)
      {
         if (releaseKey >= 528040)
            return "4.8 or later";
         if (releaseKey >= 461808)
            return "4.7.2";
         if (releaseKey >= 461308)
            return "4.7.1";
         if (releaseKey >= 460798)
            return "4.7";
         if (releaseKey >= 394802)
            return "4.6.2";
         if (releaseKey >= 394254)
            return "4.6.1";      
         if (releaseKey >= 393295)
            return "4.6";      
         if (releaseKey >= 379893)
            return "4.5.2";      
         if (releaseKey >= 378675)
            return "4.5.1";      
         if (releaseKey >= 378389)
            return "4.5";      
         // This code should never execute. A non-null release key should mean
         // that 4.5 or later is installed.
         return "No 4.5 or later version detected";
      }
   }
}

The output is like this:

.NET Runtime Version: 4.0.30319.42000
.NET Framework Version: 4.6.2
CsvHelper fullname:
   CsvHelper, Version=12.0.0.0, Culture=neutral, PublicKeyToken=8c4959082be5c823
ValueTuple fullname:
   System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
ValueTuple location:
   C:\ProgramData\CS-Script\nuget\CsvHelper\System.ValueTuple.4.4.0\lib\net47\System.ValueTuple.dll
Try to use CsvHelper library, in which has System.ValueTuple dependency.
Error: Specified file could not be executed.

System.TypeLoadException: Could not load type 'System.ValueTuple`2' from assembly 'System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
   at CsvHelper.ReadingContext..ctor(TextReader reader, Configuration configuration, Boolean leaveOpen)
   at CsvHelper.CsvReader..ctor(TextReader reader)
   at GameScriptBuilder.Main(String[] args)

CsvHelper in my computer decides to load the ValueTuple assembly built for dotnet 4.7 (in which doesn't have any ValueType class because dotnet 4.7 already has ValueTuple classes) instead of the correct assembly for dotnet 4.6 (in C:\ProgramData\CS-Script\nuget\CsvHelper\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll). If I manually copy net461\System.ValueTuple.dll into the same folder of my script then the script can run properly. Is it a bug in Csscript or it's just CsvHelper bug?

That's a useful answer
Without any help

Bingo! :smile:

This is what one would do when referencing very precisely the package assemblies "from the script side"

//css_nuget -noref CsvHelper
//css_ref %css_nuget%\CsvHelper\CsvHelper.12.1.2\lib\net45\CsvHelper.dll
//css_ref %css_nuget%\CsvHelper\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll

The first line ensures the package is downloaded. The next two reference the two required assemblies.

And if you want to use simplified version:

//css_nuget -noref CsvHelper
//css_ref %css_nuget%\**\net45\CsvHelper.dll
//css_ref %css_nuget%\**\net461\System.ValueTuple.dll