Compiler Issue in Visual Basic Project With Microsoft.CodeAnalysis.Emit

大兔子大兔子 提交于 2019-12-25 19:04:27

问题


I have developed following code to generate dll files using Microsoft.CodeAnalysis.Emit library. The code successfully generates dll files for C# projects. However it doesn't successfully build Visual Basic projects. It throws lot of compiler errors for VB projects which build successfully using the VS IDE. Please see the errors thrown for a basic Windows application project. Is there any specific compiler options for VB projects? Please advice how to resolve this.

Microsoft.CodeAnalysis package version: 1.3.1

class Program
{
    static void Main(string[] args)
    {
        const string UnitTestArtifactFolder = @"c:\VSUnitTest";
        string SolutionPath = @"C:\B\VBWinApp\VBWinApp\VBWinApp.vbproj";
        CompileProject(SolutionPath, UnitTestArtifactFolder);
    }

    private static void CompileProject(string projectFilePath, string outputFolderPath)
    {
        using (var workspace = MSBuildWorkspace.Create())
        {
            var project = workspace.OpenProjectAsync(projectFilePath).Result;
            Emit(project, outputFolderPath).Wait();
        }
    }

    private static async Task Emit(Project project, string outputFolderPath)
    {
        Directory.CreateDirectory(outputFolderPath);
        var options = GetCompilationOptions(project);
        var compilation = await project.GetCompilationAsync();
        compilation = compilation.WithOptions(options);
        var outputFilePath = Path.Combine(outputFolderPath, Path.GetFileName(project.OutputFilePath));
        var pdbFilePath = Path.ChangeExtension(outputFilePath, "pdb");
        //LogInfo("Compiling the project...");
        var compilationStatus = compilation.Emit(outputFilePath, pdbPath: pdbFilePath);

        if (!compilationStatus.Success)
        {
            //LogError(compilationStatus.Diagnostics.First(k => k.WarningLevel == 0));
        }
        else
        {
           // LogInfo("Compilation successful.");
        }
    }

    private static CompilationOptions GetCompilationOptions(Project project)
    {
        if (project.Language == "C#")
        {
            //LogInfo("Using C# Compilation Options");
            return new CSharpCompilationOptions
            (OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug);
        }
        else if (project.Language == "Visual Basic")
        {
            //LogInfo("Using Visual Basic Compilation Options");
            return new VisualBasicCompilationOptions
                (OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug);
        }
        else
        {
            //Throw if the language is other than C# or VB
            throw new InvalidOperationException("Unsupported Language.");
        }
    }
}

Compiler errors on basic Windows app.

[0] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(34) : error BC30284: sub 'OnCreateMainForm' cannot be declared 'Overrides' because it does not override a sub in a base class. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [1] C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(67) : error BC30002: Type 'Global.VBWinApp.My.MySettings' is not defined. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [2] C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(69) : error BC30456: 'VBWinApp' is not a member of 'Global'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [3] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(26) : error BC30057: Too many arguments to 'Public Overloads Sub New()'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [4] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(27) : error BC30456: 'IsSingleInstance' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [5] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(28) : error BC30456: 'EnableVisualStyles' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [6] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(29) : error BC30456: 'SaveMySettingsOnExit' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [7] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(30) : error BC30456: 'ShutDownStyle' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [8] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(35) : error BC30456: 'MainForm' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [9] C:\B\VBWinApp\VBWinApp\My Project\Application.Designer.vb(35) : error BC30456: 'VBWinApp' is not a member of 'Global'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [10]C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(33) : error BC30456: 'Application' is not a member of 'My'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [11]C:\B\VBWinApp\VBWinApp\My Project\Settings.Designer.vb(47) : error BC30456: 'Application' is not a member of 'My'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [12]C:\B\VBWinApp\VBWinApp\My Project\AssemblyInfo.vb(1) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [13]C:\Users\xxxxx\AppData\Local\Temp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb(4) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [14]C:\Users\xxxxx\AppData\Local\Temp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb(5) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic}


回答1:


The issue is that by setting compilation options, you're throwing away all options that come from the project. If you just comment out the line compilation = compilation.WithOptions(options);, compilation should succeed (at least it does for me for a newly created VB.NET WinForms project).



来源:https://stackoverflow.com/questions/43227185/compiler-issue-in-visual-basic-project-with-microsoft-codeanalysis-emit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!