Visual Studio Test vs Form application

0

This is Visual Studio 2017 V15.5.2, and none of the proposed fixes apply.

I have a simple test which fails to execute with an exception on the "Dim comp" line.

System.BadImageFormatException: 'Could not load file or assembly 'System.IO.FileSystem, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)'

Test code below but the almost identical code when copied to a Windows Forms Application works perfectly.

Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Xunit
Public Class ExpressionTests
    <Fact>
    Public Sub SystemConvert()
        Dim tree As SyntaxTree = VisualBasicSyntaxTree.ParseText(
"Imports System
Imports System.Collections.Generic
Imports System.Text
Class TestClass
    Private Sub TestMethod()
        Dim x = ""Hello, World!""
    End Sub
End Class")

Dim comp As Compilation = VisualBasicCompilation.Create("HelloWorld").
            AddReferences(MetadataReference.CreateFromFile(GetType(Object).Assembly.Location),
                          MetadataReference.CreateFromFile(GetType(ExpressionTests).Assembly.Location)).
                          AddSyntaxTrees(tree)
        Dim model As SemanticModel = comp.GetSemanticModel(tree)
    End Sub
End Class

Form Code below

Public Class Form1
    Dim tree As SyntaxTree = VisualBasicSyntaxTree.ParseText("Imports System
        Imports System.Collections.Generic
        Imports System.Text
        Class TestClass
            Private Sub TestMethod()
                Dim x = ""Hello, World!""
        End Sub
    End Class")

    Dim comp As Compilation = VisualBasicCompilation.Create("HelloWorld").
            AddReferences(MetadataReference.CreateFromFile(GetType(Object).Assembly.Location),
                          MetadataReference.CreateFromFile(GetType(Form1).Assembly.Location)).
                          AddSyntaxTrees(tree)

    Dim model As SemanticModel = comp.GetSemanticModel(tree)
End Class
vb.net
visual-studio
xunit.net
asked on Stack Overflow Dec 31, 2017 by Paul Cohen • edited Jan 1, 2018 by Paul Cohen

1 Answer

0

With some help from Suni and one additional step I have a solution. Step 1 change target framework of Test Project (I changed from 4.7.1 to 4.6.2 and then back to 4.7.1). STEP 2!!! in the NuGet Command Windows type Update-Package -reinstall. When that completes everything works.

answered on Stack Overflow Jan 1, 2018 by Paul Cohen

User contributions licensed under CC BY-SA 3.0