Пример #1
0
        static async Task <ExportProvider> CreateExportProviderAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance);
            var parts     = await discovery.CreatePartsAsync(Assembly.GetExecutingAssembly(), cancellationToken);

            var catalog = ComposableCatalog.Create(Resolver.DefaultInstance)
                          .AddParts(parts);
            var composition = CompositionConfiguration.Create(catalog);

            composition.CreateDgml().Save("mefgraph.dgml");
            if (!composition.CompositionErrors.IsEmpty)
            {
                foreach (var error in composition.CompositionErrors.Peek())
                {
                    Console.Error.WriteLine(error.Message);
                    foreach (var part in error.Parts)
                    {
                        Console.Error.WriteLine("   " + part.Definition.Type.FullName);
                    }
                }
            }

            composition.ThrowOnErrors();
            var exportProviderFactory = composition.CreateExportProviderFactory();

            return(exportProviderFactory.CreateExportProvider());
        }
        static GenericAnalyzerTest()
        {
            string codeAnalysisTestVersion =
                typeof(Compilation).Assembly.GetName().Version.Major switch
            {
                1 => "1.2.1",
                2 => "2.8.2",
                3 => "3.3.1",
                _ => throw new InvalidOperationException("Unknown version."),
            };

            ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
                                                                              new PackageIdentity("Microsoft.CodeAnalysis.CSharp", codeAnalysisTestVersion),
                                                                              new PackageIdentity("System.ValueTuple", "4.5.0")));

            ExportProviderFactory = new Lazy <IExportProviderFactory>(
                () =>
            {
                var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
                var parts     = Task.Run(() => discovery.CreatePartsAsync(MefHostServices.DefaultAssemblies)).GetAwaiter().GetResult();
                var catalog   = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);

                var configuration      = CompositionConfiguration.Create(catalog);
                var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
                return(runtimeComposition.CreateExportProviderFactory());
            },
                LazyThreadSafetyMode.ExecutionAndPublication);
        }
        private static ComposableCatalog CreateCatalog(ImmutableArray <Assembly> assemblies)
        {
            var resolver  = new Resolver(SimpleAssemblyLoader.Instance);
            var discovery = new AttributedPartDiscovery(resolver, isNonPublicSupported: true);
            var parts     = Task.Run(async() => await discovery.CreatePartsAsync(assemblies).ConfigureAwait(false)).GetAwaiter().GetResult();

            return(ComposableCatalog.Create(resolver).AddParts(parts));
        }
        private static async Task <IExportProviderFactory> CreateExportProviderFactoryAsync(ImmutableArray <Assembly> assemblies)
        {
            var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
            var parts     = await discovery.CreatePartsAsync(assemblies);

            var catalog = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);

            var configuration      = CompositionConfiguration.Create(catalog);
            var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);

            return(runtimeComposition.CreateExportProviderFactory());
        }
Пример #5
0
        static AbstractFormatterTest()
        {
            ExportProviderFactory = new Lazy <IExportProviderFactory>(
                () =>
            {
                var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
                var parts     = Task.Run(() => discovery.CreatePartsAsync(MefHostServices.DefaultAssemblies)).GetAwaiter().GetResult();
                var catalog   = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);

                var configuration      = CompositionConfiguration.Create(catalog);
                var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
                return(runtimeComposition.CreateExportProviderFactory());
            },
                LazyThreadSafetyMode.ExecutionAndPublication);
        }
Пример #6
0
    static CodeFixProviderDiscovery()
    {
        _exportProviderFactory = new Lazy <IExportProviderFactory>(
            () =>
        {
            var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
            var parts     = Task.Run(() => discovery.CreatePartsAsync(typeof(SystemConsoleToAnsiConsoleFix).Assembly)).GetAwaiter().GetResult();
            var catalog   = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts);

            var configuration      = CompositionConfiguration.Create(catalog);
            var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
            return(runtimeComposition.CreateExportProviderFactory());
        },
            LazyThreadSafetyMode.ExecutionAndPublication);
    }
        /// <inheritdoc cref="Microsoft.CodeAnalysis.Testing.AnalyzerTest{IVerifier}"/>>
        static DiagnosticAnalyzerRunner()
        {
            ExportProviderFactory = new Lazy <IExportProviderFactory>(
                () =>
            {
                var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, true);
                var parts     = Task.Run(() => discovery.CreatePartsAsync(MefHostServices.DefaultAssemblies))
                                .GetAwaiter().GetResult();
                var catalog = ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts)
                              .WithDocumentTextDifferencingService();

                var configuration      = CompositionConfiguration.Create(catalog);
                var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
                return(runtimeComposition.CreateExportProviderFactory());
            },
                LazyThreadSafetyMode.ExecutionAndPublication
                );
        }
Пример #8
0
        public async Task StaticFactoryMethodCanCreateMEFPart()
        {
            var discoverer             = new AttributedPartDiscovery(Resolver.DefaultInstance, isNonPublicSupported: true);
            var someOtherExportPart    = discoverer.CreatePart(typeof(SomeOtherExport));
            var staticFactoryPart      = discoverer.CreatePart(typeof(MEFPartWithStaticFactoryMethod));
            var staticFactoryMethodRef = MethodRef.Get(typeof(MEFPartWithStaticFactoryMethod).GetTypeInfo().DeclaredMethods.Single(m => m.Name == nameof(MEFPartWithStaticFactoryMethod.Create)), Resolver.DefaultInstance);

            staticFactoryPart = new ComposablePartDefinition(
                staticFactoryPart.TypeRef,
                staticFactoryPart.Metadata,
                staticFactoryPart.ExportedTypes,
                staticFactoryPart.ExportingMembers,
                staticFactoryPart.ImportingMembers,
                staticFactoryPart.SharingBoundary,
                staticFactoryPart.OnImportsSatisfiedRef,
                staticFactoryMethodRef,
                staticFactoryPart.ImportingConstructorImports.Take(1).ToList(),
                staticFactoryPart.CreationPolicy,
                staticFactoryPart.IsSharingBoundaryInferred);

            var catalog = ComposableCatalog.Create(Resolver.DefaultInstance)
                          .AddParts(new[] { someOtherExportPart, staticFactoryPart });
            var configuration = CompositionConfiguration.Create(catalog);

            if (!configuration.CompositionErrors.IsEmpty)
            {
                foreach (var error in configuration.CompositionErrors.Peek())
                {
                    this.logger.WriteLine(error.Message);
                }

                configuration.ThrowOnErrors();
            }

            var container = await configuration.CreateContainerAsync(this.logger);

            SomeOtherExport anotherExport          = container.GetExportedValue <SomeOtherExport>();
            MEFPartWithStaticFactoryMethod mefPart = container.GetExportedValue <MEFPartWithStaticFactoryMethod>();

            Assert.NotNull(mefPart.SomeOtherExport);
            Assert.Same(anotherExport, mefPart.SomeOtherExport);
            Assert.True(mefPart.AnotherRandomValue);
        }
Пример #9
0
        public IComposition CreateComposition()
        {
            var context    = plugins.Load();
            var assemblies = context.GetAssemblies();

            // See https://github.com/microsoft/vs-mef/blob/master/doc/hosting.md
            var discovery = new AttributedPartDiscovery(Resolver.DefaultInstance, true);
            var catalog   = ComposableCatalog.Create(Resolver.DefaultInstance)
                            .AddParts(discovery.CreatePartsAsync(Assembly.GetExecutingAssembly()).Result)
                            .AddParts(discovery.CreatePartsAsync(typeof(IShell).Assembly).Result)
                            .WithCompositionService();

            // Add parts from plugins
            foreach (var assembly in assemblies)
            {
                catalog = catalog.AddParts(discovery.CreatePartsAsync(assembly).Result);
            }

            foreach (var assemblyFile in catalog.DiscoveredParts.DiscoveryErrors.GroupBy(x => x.AssemblyPath).Select(x => x.Key))
            {
                plugins.Disable(assemblyFile);
            }

            var config = CompositionConfiguration.Create(catalog);

            foreach (var assembly in config.CompositionErrors
                     .SelectMany(error => error
                                 .SelectMany(diagnostic => diagnostic.Parts
                                             .Select(part => part.Definition.Type.Assembly)))
                     .Distinct())
            {
                plugins.Disable(assembly);
            }

            var provider = config.CreateExportProviderFactory().CreateExportProvider();

            return(new Composition(provider, context));
        }