/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private static Project CreateProject(string[] sources, Version dotNetVersion, string language = LanguageNames.CSharp, IEnumerable <MetadataReference> references = null) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var options = language == LanguageNames.CSharp ? CSharpDefaultOptions : VisualBasicDefaultOptions; var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var refAssemblies = ReferenceAssemblies.GetCache(dotNetVersion ?? new Version(4, 5, 2)); var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) // Todo: rework assembly references for .NET Core, because currently mscorlib is always referenced // need to reference nuget packages like .AddPackages in https://github.com/dotnet/roslyn-analyzers/blob/master/src/Test.Utilities/AdditionalMetadataReferences.cs .AddMetadataReference(projectId, refAssemblies.GetMetadata("mscorlib.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.Core.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.Xml.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.Data.dll")) .AddMetadataReference(projectId, CodeAnalysisReference) .WithProjectCompilationOptions(projectId, options); if (references != null) { solution = solution.AddMetadataReferences(projectId, references); } int count = 0; foreach (var source in sources) { var newFileName = $"{fileNamePrefix}{count}.{fileExt}"; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } var newProject = solution.GetProject(projectId); return(newProject); //var parseOptions = newProject.ParseOptions.WithFeatures( // newProject.ParseOptions.Features.Concat( // new[] { new KeyValuePair<string, string>("flow-analysis", "true") })); //return newProject.WithParseOptions(parseOptions); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private static Project CreateProject(string[] sources, Version dotNetVersion, string language = LanguageNames.CSharp, IEnumerable <MetadataReference> references = null) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var options = language == LanguageNames.CSharp ? CSharpDefaultOptions : VisualBasicDefaultOptions; var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var refAssemblies = ReferenceAssemblies.GetCache(dotNetVersion ?? new Version(4, 5, 2)); var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .AddMetadataReference(projectId, refAssemblies.GetMetadata("mscorlib.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.Core.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.Xml.dll")) .AddMetadataReference(projectId, refAssemblies.GetMetadata("System.Data.dll")) .AddMetadataReference(projectId, CodeAnalysisReference) .WithProjectCompilationOptions(projectId, options); if (references != null) { solution = solution.AddMetadataReferences(projectId, references); } int count = 0; foreach (var source in sources) { var newFileName = fileNamePrefix + count + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } return(solution.GetProject(projectId)); }