Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c# - Compiler Issue in Visual Basic Project With Microsoft.CodeAnalysis.Emit

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:BVBWinAppVBWinAppVBWinApp.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:BVBWinAppVBWinAppMy ProjectApplication.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:BVBWinAppVBWinAppMy ProjectSettings.Designer.vb(67) : error BC30002: Type 'Global.VBWinApp.My.MySettings' is not defined. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [2] C:BVBWinAppVBWinAppMy ProjectSettings.Designer.vb(69) : error BC30456: 'VBWinApp' is not a member of 'Global'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [3] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(26) : error BC30057: Too many arguments to 'Public Overloads Sub New()'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [4] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(27) : error BC30456: 'IsSingleInstance' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [5] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(28) : error BC30456: 'EnableVisualStyles' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [6] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(29) : error BC30456: 'SaveMySettingsOnExit' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [7] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(30) : error BC30456: 'ShutDownStyle' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [8] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(35) : error BC30456: 'MainForm' is not a member of 'MyApplication'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [9] C:BVBWinAppVBWinAppMy ProjectApplication.Designer.vb(35) : error BC30456: 'VBWinApp' is not a member of 'Global'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [10]C:BVBWinAppVBWinAppMy ProjectSettings.Designer.vb(33) : error BC30456: 'Application' is not a member of 'My'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [11]C:BVBWinAppVBWinAppMy ProjectSettings.Designer.vb(47) : error BC30456: 'Application' is not a member of 'My'. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [12]C:BVBWinAppVBWinAppMy ProjectAssemblyInfo.vb(1) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [13]C:UsersxxxxxAppDataLocalTemp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb(4) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic} [14]C:UsersxxxxxAppDataLocalTemp.NETFramework,Version=v4.5.2.AssemblyAttributes.vb(5) : hidden BC50001: Unused import statement. Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...