我希望能够使用Roslyn编译单个VB.Net文件,我发现的每个示例都有不同的方法来获取引用,使用不同的API并且它们都不会编译我提供给我的应用程序的每个程序。以下是我发现的一些参考方法。我不明白编译程序错误实际上需要什么。有些程序有导入,我是否需要传递被引用到编译器的DLL?如何找到他们NuGet Reference的内容?下面的一些示例(比如包括CodeAnalysisReference)给我的错误多于不使用任何错误。我不想运行正在编译的文件,我只想获得诊断错误。是否有任何文档解释了如何将参考传递给Roslyn工作以及需要传递什么?
Dim CodeAnalysisReference As MetadataReference = MetadataReference.CreateFromFile(GetType(Compilation).Assembly.Location)
Dim VisualBasicSymbolsReference As MetadataReference = MetadataReference.CreateFromFile(GetType(VisualBasicCompilation).Assembly.Location)
Dim runtimePath As String = "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.1\{0}.dll"
Dim DefaultReferences As IEnumerable(Of MetadataReference) = {
CodeAnalysisReference,
VisualBasicSymbolsReference,
MetadataReference.CreateFromFile(String.Format(runtimePath, "System")),
MetadataReference.CreateFromFile(String.Format(runtimePath, "System.Core"))
}
Dim ActionReferences As MetadataReference = MetadataReference.CreateFromFile(GetType(Action).Assembly.Location())
Dim ComponentModelReferences As MetadataReference = MetadataReference.CreateFromFile(GetType(ComponentModel.EditorBrowsableAttribute).Assembly.Location())
Dim CorlibReference As MetadataReference = MetadataReference.CreateFromFile(GetType(Object).Assembly.Location)
Dim EnumerableReferences As MetadataReference = MetadataReference.CreateFromFile(GetType(Enumerable).Assembly.Location())
Dim MSCorLib As MetadataReference = MetadataReference.CreateFromFile(String.Format(runtimePath, "mscorlib"))
Dim systemReference As PortableExecutableReference = AppDomain.CurrentDomain.GetAssemblies().Where(Function(x As Reflection.Assembly) String.Equals(x.GetName().Name, NameOf(System), StringComparison.OrdinalIgnoreCase)).Select(Function(a As Reflection.Assembly) MetadataReference.CreateFromFile(a.Location)).Single()
Dim vbRuntime As PortableExecutableReference = MetadataReference.CreateFromFile(GetType(CompilerServices.StandardModuleAttribute).Assembly.Location)
Dim references() As MetadataReference = {ActionReferences, ComponentModelReferences, CorlibReference, EnumerableReferences, MSCorLib, systemReference, vbRuntime}