Пример #1
0
        [ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
        public void BasicTest()
        {
            var catalog = CatalogFactory.CreateDefaultAttributed();
            var catalogExportProvider = new CatalogExportProvider(catalog);

            catalogExportProvider.SourceProvider = catalogExportProvider;
            var testName             = AttributedModelServices.GetContractName(typeof(CatalogComponentTest));
            var testNameNonComponent = AttributedModelServices.GetContractName(typeof(CatalogComponentTestNonComponentPart));
            var testInterfaceName    = AttributedModelServices.GetContractName(typeof(ICatalogComponentTest));

            Assert.Equal(1, catalogExportProvider.GetExports(ImportFromContract(testName)).Count());
            Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContract(testNameNonComponent)).Count());

            var exports = catalogExportProvider.GetExports(ImportFromContract(testInterfaceName));

            Assert.Equal(2, exports.Count());

            foreach (var i in exports)
            {
                Assert.NotNull(i.Value);
            }
        }
Пример #2
0
        public void TryToDiscoverExportWithGenericParameter()
        {
            var catalog   = new AssemblyCatalog(typeof(AssemblyCatalogTests).Assembly);
            var container = new CompositionContainer(catalog);

            // Open generic should fail because we should discover that type
            Assert.IsNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWithGenericParameter <>))));

            // This specific generic was not exported any where so it should fail
            Assert.IsNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWithGenericParameter <double>))));

            // This specific generic was exported so it should succeed
            Assert.IsNotNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWithGenericParameter <int>))));

            // Shouldn't discovoer static type with open generic
            Assert.IsNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(StaticExportWithGenericParameter <>))));

            // Should find a type that inherits from an export
            Assert.IsNotNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWhichInheritsFromGeneric))));

            // This should be exported because it is inherited by ExportWhichInheritsFromGeneric
            Assert.IsNotNull(container.GetExportedValueOrDefault <object>(AttributedModelServices.GetContractName(typeof(ExportWithGenericParameter <string>))));
        }
Пример #3
0
 public MicroExport(Type contractType, IDictionary <string, object> metadata, params object[] exportedValues)
     : this(AttributedModelServices.GetContractName(contractType), exportedValues[0].GetType(), metadata, exportedValues)
 {
 }
Пример #4
0
 public MicroExport(Type contractType, params object[] exportedValues)
     : this(AttributedModelServices.GetContractName(contractType), contractType, (IDictionary <string, object>)null, exportedValues)
 {
 }
Пример #5
0
 private static string NameForType <T>()
 {
     return(AttributedModelServices.GetContractName(typeof(T)));
 }
Пример #6
0
 private Export GetSingleLazy <T>(ComposablePart part)
 {
     return(this.GetSingleExport(part, AttributedModelServices.GetContractName(typeof(T))));
 }
Пример #7
0
        public void GetExports()
        {
            var catalog = new AggregateCatalog();
            Expression <Func <ExportDefinition, bool> > constraint = (ExportDefinition exportDefinition) => exportDefinition.ContractName == AttributedModelServices.GetContractName(typeof(MyExport));
            IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> > matchingExports = null;

            matchingExports = catalog.GetExports(constraint);
            Assert.NotNull(matchingExports);
            Assert.True(matchingExports.Count() == 0);

            var testsDirectoryCatalog = new DirectoryCatalog(TemporaryFileCopier.GetTemporaryDirectory());

            catalog.Catalogs.Add(testsDirectoryCatalog);
            matchingExports = catalog.GetExports(constraint);

            Assert.NotNull(matchingExports);
            Assert.True(matchingExports.Count() >= 0);

            IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> > expectedMatchingExports = catalog.Parts
                                                                                                        .SelectMany(part => part.ExportDefinitions, (part, export) => new Tuple <ComposablePartDefinition, ExportDefinition>(part, export))
                                                                                                        .Where(partAndExport => partAndExport.Item2.ContractName == AttributedModelServices.GetContractName(typeof(MyExport)));

            Assert.True(matchingExports.SequenceEqual(expectedMatchingExports));

            catalog.Catalogs.Remove(testsDirectoryCatalog);
            matchingExports = catalog.GetExports(constraint);
            Assert.NotNull(matchingExports);
            Assert.True(matchingExports.Count() == 0);
        }
Пример #8
0
 private static ImportDefinition CreateImportDefinition(Type type, string metadataKey)
 {
     return(new ContractBasedImportDefinition(AttributedModelServices.GetContractName(typeof(IMyExporter)), null, new KeyValuePair <string, Type>[] { new KeyValuePair <string, Type>(metadataKey, typeof(object)) }, ImportCardinality.ZeroOrMore, true, true, CreationPolicy.Any));
 }
Пример #9
0
        public void GetExports()
        {
            var catalog = new AssemblyCatalog(typeof(AssemblyCatalogTests).Assembly);
            Expression <Func <ExportDefinition, bool> > constraint = (ExportDefinition exportDefinition) => exportDefinition.ContractName == AttributedModelServices.GetContractName(typeof(MyExport));
            IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> > matchingExports = catalog.GetExports(constraint);

            Assert.IsNotNull(matchingExports);
            Assert.IsTrue(matchingExports.Count() >= 0);

            IEnumerable <Tuple <ComposablePartDefinition, ExportDefinition> > expectedMatchingExports = catalog.Parts
                                                                                                        .SelectMany(part => part.ExportDefinitions, (part, export) => new Tuple <ComposablePartDefinition, ExportDefinition>(part, export))
                                                                                                        .Where(partAndExport => partAndExport.Item2.ContractName == AttributedModelServices.GetContractName(typeof(MyExport)));

            Assert.IsTrue(matchingExports.SequenceEqual(expectedMatchingExports));
        }
Пример #10
0
        public static CompositionScopeDefinition AsScopeWithPublicSurface <T>(this ComposablePartCatalog catalog, params CompositionScopeDefinition[] children)
        {
            IEnumerable <ExportDefinition> definitions = catalog.Parts.SelectMany((p) => p.ExportDefinitions.Where((e) => e.ContractName == AttributedModelServices.GetContractName(typeof(T))));

            return(new CompositionScopeDefinition(catalog, children, definitions));
        }