我开发了以下代码,使用Microsoft.CodeAnalysis.Emit库生成dll文件。代码成功生成C#项目的dll文件。但是它无法成功构建Visual Basic项目。它为使用VS IDE成功构建的VB项目抛出了很多编译器错误。请查看基本Windows应用程序项目引发的错误。 VB项目是否有任何特定的编译器选项?请告知如何解决此问题。
Microsoft.CodeAnalysis包版本: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.");
}
}
}
基本Windows应用程序上的编译器错误。
[0] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(34): 错误BC30284:sub'OnCreateMainForm'无法声明'Overrides' 因为它不会覆盖基础中的子 类。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [1] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Settings.Designer.vb(67):错误 BC30002:输入'Global.VBWinApp.My.MySettings'不是 定义。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [2] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Settings.Designer.vb(69):错误 BC30456:'VBWinApp'不是其成员 '全球'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [3] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(26): 错误BC30057:'Public Overloads Sub的参数太多 新()'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [4] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(27): 错误BC30456:'IsSingleInstance'不是。的成员 '我的应用程序'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [5] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(28): 错误BC30456:'EnableVisualStyles'不是其成员 '我的应用程序'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [6] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(29): 错误BC30456:'SaveMySettingsOnExit'不是其成员 '我的应用程序'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [7] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(30): 错误BC30456:'ShutDownStyle'不是。的成员 '我的应用程序'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [8] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(35): 错误BC30456:'MainForm'不是其成员 '我的应用程序'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [9] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Application.Designer.vb(35): 错误BC30456:'VBWinApp'不是其成员 '全球'。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [10] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Settings.Designer.vb(33):错误 BC30456:'申请'不是其成员 “我的”。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [11] C:\ B \ VBWinApp \ VBWinApp \ My Project \ Settings.Designer.vb(47):错误 BC30456:'申请'不是其成员 “我的”。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [12] C:\ B \ VBWinApp \ VBWinApp \ My Project \ AssemblyInfo.vb(1):hidden BC50001:未使用的导入语句。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [13] C:\用户\ XXXXX \应用程序数据\本地\ Temp.NETFramework,版本= v4.5.2.AssemblyAttributes.vb(4) :隐藏BC50001:未使用导入 声明。 Microsoft.CodeAnalysis.Diagnostic {} Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic [14] C:\用户\ XXXXX \应用程序数据\本地\ Temp.NETFramework,版本= v4.5.2.AssemblyAttributes.vb(5) :隐藏BC50001:未使用导入 声明。 Microsoft.CodeAnalysis.Diagnostic {Microsoft.CodeAnalysis.VisualBasic.VBDiagnostic}
答案 0 :(得分:4)
问题在于,通过设置编译选项,您将丢弃来自项目的所有选项。如果你只注释掉行compilation = compilation.WithOptions(options);
,那么编译应该会成功(至少对我来说是新创建的VB.NET WinForms项目)。