Пример #1
0
        private static MetadataReference ProcessAssemblyReference(AssemblyReference assemblyReference)
        {
            if (assemblyReference.NuGet == null)
            {
                var assemblyPath = Path.Combine(systemAssembliesFolder, assemblyReference.Name);
                return(MetadataReference.CreateFromFile(assemblyPath));
            }

            packageManager.InstallPackage(assemblyReference.NuGet.Name, assemblyReference.NuGet.Version,
                                          ignoreDependencies: true, allowPrereleaseVersions: false);

            var matchingDlls = Directory.GetFiles(GetNuGetPackageDirectory(assemblyReference.NuGet),
                                                  assemblyReference.Name, SearchOption.AllDirectories)
                               .Select(x => new FileInfo(x))
                               .Where(x => allowedNugetLibDirectories.Any(y => x.Directory.Name.StartsWith(y)))
                               .OrderByDescending(x => x.FullName);

            return(MetadataReference.CreateFromFile(matchingDlls.First().FullName));
        }
Пример #2
0
 public static (SyntaxTree, SemanticModel) Compile(string classDeclaration, bool isCSharp = true,
                                                   params AssemblyReference[] references)
 {
     using (var workspace = new AdhocWorkspace())
     {
         var document = workspace.CurrentSolution.AddProject("project", "project.dll", isCSharp ? LanguageNames.CSharp : LanguageNames.VisualBasic)
                        .AddMetadataReference(AssemblyReferenceService.GetMetadataReference(AssemblyReference.FromFramework("mscorlib.dll")))
                        .AddMetadataReference(AssemblyReferenceService.GetMetadataReference(AssemblyReference.FromFramework("System.dll")))
                        .AddMetadataReference(AssemblyReferenceService.GetMetadataReference(AssemblyReference.FromFramework("System.Core.dll")))
                        .AddMetadataReferences(references.Select(AssemblyReferenceService.GetMetadataReference))
                        .AddDocument("Class1", classDeclaration);
         var compilation = document.Project.GetCompilationAsync().Result;
         var tree        = compilation.SyntaxTrees.First();
         return(tree, compilation.GetSemanticModel(tree));
     }
 }
Пример #3
0
        private static Project CreateProject(string fileExtension, string assemblyName, AdhocWorkspace workspace,
                                             params AssemblyReference[] additionalReferences)
        {
            var language = fileExtension == CSharpFileExtension
                ? LanguageNames.CSharp
                : LanguageNames.VisualBasic;

            var project = workspace.CurrentSolution.AddProject(assemblyName, $"{assemblyName}.dll", language)
                          .AddMetadataReference(AssemblyReferenceService.GetMetadataReference(AssemblyReference.FromFramework("mscorlib.dll")))
                          .AddMetadataReference(AssemblyReferenceService.GetMetadataReference(AssemblyReference.FromFramework("System.dll")))
                          .AddMetadataReference(AssemblyReferenceService.GetMetadataReference(AssemblyReference.FromFramework("System.Core.dll")))
                          .AddMetadataReferences(additionalReferences.Select(AssemblyReferenceService.GetMetadataReference));

            // adding an extra file to the project
            // this won't trigger any issues, but it keeps a reference to the original ParseOption, so
            // if an analyzer/codefix changes the language version, Roslyn throws an ArgumentException
            project = project.AddDocument("ExtraEmptyFile.g" + fileExtension, string.Empty).Project;

            return(project);
        }
Пример #4
0
 public static MetadataReference GetMetadataReference(AssemblyReference assemblyReference) =>
 cache.GetOrAdd(assemblyReference, ProcessAssemblyReference);