这是Visual Studio 2017 V15.5.2,并且所有修订都不适用。
我有一个简单的测试,无法在“Dim comp”行上执行异常。
System.BadImageFormatException:'无法加载文件或程序集'System.IO.FileSystem,Version = 4.0.1.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。不应加载引用程序集以执行。它们只能在Reflection-only loader上下文中加载。 (来自HRESULT的异常:0x80131058)'
下面测试代码,但复制到Windows窗体应用程序时几乎完全相同的代码可以正常工作。
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
下面的表格代码
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
答案 0 :(得分:0)
在Suni的帮助下,我还有一个解决方案。第1步更改测试项目的目标框架(我从4.7.1更改为4.6.2,然后再更改为4.7.1)。第2步!!!在NuGet Command Windows类型Update-Package -reinstall中。完成后一切正常。