dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.' #28506

Open MattWolf74 opened 6 years ago

MattWolf74 commented 6 years ago

I use a Console app (.Net Target Framework 4.7.2) and imported (via Nuget) Microsoft.CodeAnalysis.CSharp (version 2.8.2) and Microsoft.CodeAnalysis.CSharp.Scripting (version 2.8.2)

I then try to run the following simple code:

class Program
    {
        static void Main(string[] args)
        {

            var directory = @"X:\Algorithmic Trading Static Data\Plugins\ResearchStrategy2";
            var scriptCode = File.ReadAllText(directory + "\\ResearchStrategy2.cs");

            TestRun(scriptCode);

            Console.WriteLine("Press to end");
            Console.ReadLine();
        }

        public static void TestRun(string sourceCode)
        {
            var script = CSharpScript.Create(
                @"public class Test : ConsoleApp2.IRunnable {
            public void Run() {
                System.Console.WriteLine(""test"");
            }
        }
        return typeof(Test);
        ", ScriptOptions.Default.WithReferences(Assembly.GetExecutingAssembly()));

            script.Compile();

            // run and you get Type object for your fresh type
            var testType = (Type)script.RunAsync().Result.ReturnValue;

            // create and cast to interface
            var runnable = (IRunnable)Activator.CreateInstance(testType);

            // use
            runnable.Run();

            Console.ReadKey();
        }

        public interface IRunnable
        {
            void Run();
        }

    }

The following Run-Time error is thrown at

var script = CSharpScript.Create( @"public class Test : ConsoleApp2.IRunnable { public void Run() { System.Console.WriteLine(""test""); } } return typeof(Test); ", ScriptOptions.Default.WithReferences(Assembly.GetExecutingAssembly()));

ERROR: System.IO.FileNotFoundException: 'Could not load file or assembly 'System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Inner Exception FileNotFoundException: Could not load file or assembly 'System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

What am I doing wrong and how can I fix this run-time error. Do I need some binding redirections? Which and how?

I tried to add to app.config `

........` without success... Thanks
gyurisc commented 5 years ago

I am seeing this error in my project too when trying to evaluate a Roslyn script. Any ideas when this will be fixed?