Roslyn 中缺少我的命名空间 VB.NET

Missing My namespace in Roslyn VB.NET

当我尝试使用 Roslyn 编译并发出以下 VB.NET 代码时,

Module Module1
    Sub Main()

        Console.WriteLine(My.Application.Info.AssemblyName)

    End Sub
End Module

我收到以下错误

error BC30451: 'My' is not declared. It may be inaccessible due to its protection level.

MSDN mentions 编译器根据 _MYTYPE 条件编译常量的值添加了 My 命名空间。

在罗斯林,我的魔法不再可用,是吗?

我的代码:

Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.VisualBasic.CompilerServices

Module Module1
    Sub Main()

        Dim code = "Module Module1" + Environment.NewLine +
                       "Sub Main()" + Environment.NewLine +
                           "System.Console.WriteLine(My.Application.Info.AssemblyName)" + Environment.NewLine +
                       "End Sub" + Environment.NewLine +
                   "End Module"

        Dim tree = VisualBasicSyntaxTree.ParseText(code)

        Dim compilation = VisualBasicCompilation.Create("test").
            AddSyntaxTrees(tree).
            AddReferences(MetadataReference.CreateFromFile(GetType(Object).Assembly.Location)).
            AddReferences(MetadataReference.CreateFromFile(GetType(StandardModuleAttribute).Assembly.Location))

        Dim emitResult = compilation.Emit("test.exe")
        If Not emitResult.Success Then
            Console.Write(String.Join(Environment.NewLine, emitResult.Diagnostics))
        End If

        Console.ReadLine()

    End Sub
End Module

如果我理解正确,My 命名空间是通过自动包含文件添加的 VbMyTemplateText.vb into each compilation. And to make it actually generate something, the preprocessor symbol _MYTYPE 必须正确设置(在控制台应用程序的情况下 "Console").

要完成这项工作,您还需要参考 System.dll,但之后一切正常:

Imports System.CodeDom.Compiler
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.VisualBasic.CompilerServices

Module Module1

    Sub Main()
        Dim code = "Module Module1" + Environment.NewLine +
                       "Sub Main()" + Environment.NewLine +
                            "System.Console.WriteLine(My.Application.Info.AssemblyName)" + Environment.NewLine +
                       "End Sub" + Environment.NewLine +
                   "End Module"

        Dim tree = VisualBasicSyntaxTree.ParseText(code)

        Dim compilation = VisualBasicCompilation.Create("test").
            AddSyntaxTrees(tree).
            AddReferences(MetadataReference.CreateFromFile(GetType(Object).Assembly.Location)). ' mscorlib
            AddReferences(MetadataReference.CreateFromFile(GetType(GeneratedCodeAttribute).Assembly.Location)). ' System
            AddReferences(MetadataReference.CreateFromFile(GetType(StandardModuleAttribute).Assembly.Location)). ' Microsoft.VisualBasic
            WithOptions(New VisualBasicCompilationOptions(OutputKind.ConsoleApplication).WithParseOptions(
                VisualBasicParseOptions.Default.WithPreprocessorSymbols(New KeyValuePair(Of String, Object)("_MYTYPE", "Console"))))

        Dim emitResult = compilation.Emit("test.exe")
        If Not emitResult.Success Then
            Console.WriteLine(String.Join(Environment.NewLine, emitResult.Diagnostics))
        End If

    End Sub

End Module