コード例 #1
0
        /// <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, string language = LanguageNames.CSharp)
        {
            string fileNamePrefix = DefaultFilePathPrefix;
            string fileExt        = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

            var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

            var solution = new AdhocWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, language)
                           .AddMetadataReference(projectId, CorlibReference)
                           .AddMetadataReference(projectId, SystemReference)
                           .AddMetadataReference(projectId, SystemCoreReference)
                           .AddMetadataReference(projectId, CSharpSymbolsReference)
                           .AddMetadataReference(projectId, CodeAnalysisReference)
                           .AddMetadataReference(projectId, SystemRuntimeReference)
                           .AddMetadataReference(projectId, MigrationReference)
                           .AddMetadataReference(projectId, SerializationReference)
                           .AddMetadataReference(projectId, CryptoAlgoReference)
                           .AddMetadataReference(projectId, SystemDynamicRuntimeReference)
                           .AddMetadataReference(projectId, SystemLinqExpressionsReference)
                           .AddMetadataReference(projectId, NetStandard)
                           .AddMetadataReference(projectId, SystemObjectModelReference)
                           .AddMetadataReference(projectId, JsonNetReference);
            var compilationOptions = solution
                                     .GetProject(projectId)
                                     .CompilationOptions
                                     .WithOutputKind(OutputKind.DynamicallyLinkedLibrary);

            solution = solution
                       .WithProjectCompilationOptions(projectId, compilationOptions);

            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 project = solution.GetProject(projectId);

            var ignore =
                "Assuming assembly reference 'System.Linq.Expressions, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'Newtonsoft.Json' matches identity 'System.Linq.Expressions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Linq.Expressions', you may need to supply runtime policy";

            var diangostics = project.GetCompilationAsync().Result.GetDiagnostics().Where(d => !d.GetMessage().Contains(ignore));

            diangostics.Should().BeEmpty();
            return(project);
        }
コード例 #2
0
        /// <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, string language = LanguageNames.CSharp)
        {
            string fileNamePrefix = DefaultFilePathPrefix;
            string fileExt        = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;

            var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

            var solution = new AdhocWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, language)
                           .AddMetadataReference(projectId, CorlibReference)
                           .AddMetadataReference(projectId, SystemCoreReference)
                           .AddMetadataReference(projectId, CSharpSymbolsReference)
                           .AddMetadataReference(projectId, CodeAnalysisReference)
                           .AddMetadataReference(projectId, xUnitAnalysisReference)
                           .AddMetadataReference(projectId, xUnitAssertAnalysisReference)
                           .AddMetadataReference(projectId, RhinoAnalysisReference)
                           .AddMetadataReference(projectId, BusinessLogicReference)
                           .AddMetadataReference(projectId, nSubstituteReference)
                           .AddRuntimeLibrary(projectId, "netstandard.dll")
                           .AddRuntimeLibrary(projectId, "System.dll")
                           .AddRuntimeLibrary(projectId, "System.Core.dll")
                           .AddRuntimeLibrary(projectId, "mscorlib.dll")
                           .AddRuntimeLibrary(projectId, "System.Threading.Tasks.Extensions.dll")
                           .AddRuntimeLibrary(projectId, "System.Linq.Expressions.dll")
                           .AddRuntimeLibrary(projectId, "System.Runtime.dll");

            solution = solution.WithProjectCompilationOptions(projectId, solution.GetProject(projectId).CompilationOptions
                                                              .WithOutputKind(OutputKind.DynamicallyLinkedLibrary));


            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));
        }
コード例 #3
0
        private static Compilation GetCompilationForSource(string specName, string source)
        {
            var projectId  = ProjectId.CreateNewId(debugName: specName);
            var filename   = specName + ".cs";
            var documentId = DocumentId.CreateNewId(projectId, debugName: filename);

            var solution = new AdhocWorkspace().CurrentSolution
                           .AddProject(projectId, specName, specName, LanguageNames.CSharp)

                           .AddMetadataReference(projectId, CorlibReference)
                           .AddMetadataReference(projectId, SystemReference)
                           .AddMetadataReference(projectId, SystemCoreReference)
                           .AddMetadataReference(projectId, CSharpSymbolsReference)
                           .AddMetadataReference(projectId, CodeAnalysisReference)
                           .AddMetadataReference(projectId, SystemRuntimeReference)
                           .AddMetadataReference(projectId, AnnotationsReference)

                           .AddDocument(documentId, filename, SourceText.From(source));

            var compilationOptions = solution
                                     .GetProject(projectId)
                                     .CompilationOptions
                                     .WithOutputKind(OutputKind.DynamicallyLinkedLibrary);

            CSharpParseOptions parseOptions = solution
                                              .GetProject(projectId)
                                              .ParseOptions as CSharpParseOptions;

            parseOptions = parseOptions
                           .WithLanguageVersion(LanguageVersion.CSharp9);

            solution = solution
                       .WithProjectCompilationOptions(projectId, compilationOptions)
                       .WithProjectParseOptions(projectId, parseOptions);

            return(solution.Projects.First()
                   .GetCompilationAsync().Result);
        }