Esempio n. 1
0
        public RoslynCompilationWorkspace(WorkspaceConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            Configuration = configuration;

            var dependencyResolver       = configuration.DependencyResolver;
            var compilationConfiguration = configuration.CompilationConfiguration;

            workspace = new InteractiveWorkspace();
            sourceReferenceResolver      = new InteractiveSourceReferenceResolver(dependencyResolver);
            metadataReferenceResolver    = new InteractiveMetadataReferenceResolver(dependencyResolver);
            monoScriptCompilationPatcher = new MonoScriptCompilationPatcher(
                assemblyNamePrefixBytes);

            DependencyResolver = dependencyResolver;

            hostObjectType              = configuration.HostObjectType;
            EvaluationContextId         = compilationConfiguration.EvaluationContextId;
            includePeImagesInResolution = configuration.IncludePEImagesInDependencyResolution;

            initialImports             = compilationConfiguration.DefaultUsings.ToImmutableArray();
            initialWarningSuppressions = compilationConfiguration.DefaultWarningSuppressions.ToImmutableArray();
            initialDiagnosticOptions   = initialWarningSuppressions.ToImmutableDictionary(
                warningId => warningId,
                warningId => ReportDiagnostic.Suppress);
            initialReferences = dependencyResolver.ResolveDefaultReferences();

            foreach (var implicitReference in byNameImplicitReferences)
            {
                if (implicitReference == null)
                {
                    continue;
                }

                var assembly = DependencyResolver.ResolveWithoutReferences(
                    new AssemblyName(implicitReference));
                if (assembly != null)
                {
                    initialReferences = initialReferences.Add(
                        MetadataReference.CreateFromFile(assembly.Path));
                }
            }

            CompletionService = workspace
                                .Services
                                .GetLanguageServices(LanguageNames.CSharp)
                                .GetService <CompletionService> ();
        }
Esempio n. 2
0
        public RoslynCompilationWorkspace(
            InteractiveDependencyResolver dependencyResolver,
            TargetCompilationConfiguration compilationConfiguration,
            AgentType agentType,
            Type hostObjectType = null,
            bool includePeImagesInResolution = false)
        {
            if (dependencyResolver == null)
            {
                throw new ArgumentNullException(nameof(dependencyResolver));
            }

            if (compilationConfiguration == null)
            {
                throw new ArgumentNullException(nameof(compilationConfiguration));
            }

            workspace = new InteractiveWorkspace();
            sourceReferenceResolver      = new InteractiveSourceReferenceResolver(dependencyResolver);
            metadataReferenceResolver    = new InteractiveMetadataReferenceResolver(dependencyResolver);
            monoScriptCompilationPatcher = new MonoScriptCompilationPatcher(
                assemblyNamePrefixBytes);

            DependencyResolver = dependencyResolver;

            this.hostObjectType        = hostObjectType;
            EvaluationContextId        = compilationConfiguration.EvaluationContextId;
            initialImports             = compilationConfiguration.DefaultUsings.ToImmutableArray();
            initialWarningSuppressions = compilationConfiguration.DefaultWarningSuppressions.ToImmutableArray();
            initialDiagnosticOptions   = initialWarningSuppressions.ToImmutableDictionary(
                warningId => warningId,
                warningId => ReportDiagnostic.Suppress);
            initialReferences = dependencyResolver.ResolveDefaultReferences();

            var byNameImplicitReferences = new Tuple <string, Func <bool> > [] {
                Tuple.Create <string, Func <bool> > ("Microsoft.CSharp", () => true),

                // Add an implicit by name reference to System.ValueTuple if and only if the
                // agent is Console or WPF, and the current runtime does not have SVT in corlib.
                // This check makes sense because the Console and WPF agents run on the same
                // runtime as the current client. The other agents are on runtimes controlled
                // by us, and we can be sure that System.ValueTuple will be part of corlib on
                // those agents. A bug in .NET 4.7 means we can't just always implicitly add
                // the reference and have the runtime figure out what should be done. This bug
                // is fixed in .NET 4.7.1.
                Tuple.Create <string, Func <bool> > ("System.ValueTuple", () => {
                    return((agentType == AgentType.Console || agentType == AgentType.WPF) &&
                           typeof(object).Assembly.GetType("System.ValueTuple") == null);
                })
            };

            foreach (var implicitReference in byNameImplicitReferences)
            {
                if (!implicitReference.Item2())
                {
                    continue;
                }

                var assembly = DependencyResolver.ResolveWithoutReferences(
                    new AssemblyName(implicitReference.Item1));
                if (assembly != null)
                {
                    initialReferences = initialReferences.Add(
                        MetadataReference.CreateFromFile(assembly.Path));
                }
            }

            this.includePeImagesInResolution = includePeImagesInResolution;

            CompletionService = workspace
                                .Services
                                .GetLanguageServices(LanguageNames.CSharp)
                                .GetService <CompletionService> ();
        }