When I try to compile and emit the following VB.NET code using Roslyn,
Module Module1
Sub Main()
Console.WriteLine(My.Application.Info.AssemblyName)
End Sub
End Module
I get the following error
error BC30451: 'My' is not declared. It may be inaccessible due to its protection level.
MSDN mentions that My namespace is added by the compiler depending on value of the _MYTYPE conditional-compilation constant.
In Roslyn all that My magic is not available anymore, is it?
My code:
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