private IEnumerable <Export> InternalGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) { ThrowIfDisposed(); EnsureRunning(); // Use the version of the catalog appropriate to this atomicComposition ComposablePartCatalog currentCatalog = atomicComposition.GetValueAllowNull(_catalog); IPartCreatorImportDefinition partCreatorDefinition = definition as IPartCreatorImportDefinition; bool isExportFactory = false; if (partCreatorDefinition != null) { definition = partCreatorDefinition.ProductImportDefinition; isExportFactory = true; } CreationPolicy importPolicy = definition.GetRequiredCreationPolicy(); List <Export> exports = new List <Export>(); bool ensureRejection = EnsureRejection(atomicComposition); foreach (var partDefinitionAndExportDefinition in currentCatalog.GetExports(definition)) { bool isPartRejected = ensureRejection && IsRejected(partDefinitionAndExportDefinition.Item1, atomicComposition); if (!isPartRejected) { exports.Add(CreateExport(partDefinitionAndExportDefinition.Item1, partDefinitionAndExportDefinition.Item2, isExportFactory, importPolicy)); } } return(exports); }
public static ImportDefinition GetProductImportDefinition(this ImportDefinition import) { IPartCreatorImportDefinition partCreatorDefinition = import as IPartCreatorImportDefinition; if (partCreatorDefinition != null) { return(partCreatorDefinition.ProductImportDefinition); } else { return(import); } }
internal static ContractBasedImportDefinition GetPartCreatorProductImportDefinition(ImportDefinition importDefinition) { Requires.NotNull(importDefinition, "importDefinition"); IPartCreatorImportDefinition partCreatorImportDefinition = importDefinition as IPartCreatorImportDefinition; if (partCreatorImportDefinition == null) { throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidImportDefinition, importDefinition.GetType()), "importDefinition"); } return(partCreatorImportDefinition.ProductImportDefinition); }
public static ContractBasedImportDefinition GetExportFactoryProductImportDefinition(ImportDefinition importDefinition) { Requires.NotNull(importDefinition, nameof(importDefinition)); IPartCreatorImportDefinition partCreatorImportDefinition = importDefinition as IPartCreatorImportDefinition; if (partCreatorImportDefinition == null) { throw new ArgumentException( SR.Format(SR.ReflectionModel_InvalidImportDefinition, importDefinition.GetType()), nameof(importDefinition)); } return(partCreatorImportDefinition.ProductImportDefinition); }
public static ContractBasedImportDefinition GetExportFactoryProductImportDefinition(ImportDefinition importDefinition) { Requires.NotNull(importDefinition, nameof(importDefinition)); Contract.Ensures(Contract.Result <ContractBasedImportDefinition>() != null); IPartCreatorImportDefinition partCreatorImportDefinition = importDefinition as IPartCreatorImportDefinition; if (partCreatorImportDefinition == null) { throw new ArgumentException( string.Format(CultureInfo.CurrentCulture, SR.ReflectionModel_InvalidImportDefinition, importDefinition.GetType()), "importDefinition"); } return(partCreatorImportDefinition.ProductImportDefinition); }
private static ImportDefinition TranslateImport(ImportDefinition definition) { IPartCreatorImportDefinition factoryDefinition = definition as IPartCreatorImportDefinition; if (factoryDefinition == null) { return(null); } // Now we need to make sure that the creation policy is handled correctly // We will always create a new child CatalogEP to satsify the request, so from the perspecitive of the caller, the policy should // always be NonShared (or Any). From teh perspective of the callee, it's the otehr way around. ContractBasedImportDefinition productImportDefinition = factoryDefinition.ProductImportDefinition; ImportDefinition result = null; switch (productImportDefinition.RequiredCreationPolicy) { case CreationPolicy.NonShared: case CreationPolicy.NewScope: { // we need to recreate the import definition with the policy "Any", so that we can // pull singletons from the inner CatalogEP. teh "non-sharedness" is achieved through // the creation of the new EPs already. result = new ContractBasedImportDefinition( productImportDefinition.ContractName, productImportDefinition.RequiredTypeIdentity, productImportDefinition.RequiredMetadata, productImportDefinition.Cardinality, productImportDefinition.IsRecomposable, productImportDefinition.IsPrerequisite, CreationPolicy.Any, productImportDefinition.Metadata); break; } case CreationPolicy.Any: { // "Any" works every time result = productImportDefinition; break; } } return(result); }
/// <summary> /// Returns all exports that match the conditions of the specified import. /// </summary> /// <param name="definition">The <see cref="ImportDefinition"/> that defines the conditions of the /// <see cref="Export"/> to get.</param> /// <returns></returns> /// <result> /// An <see cref="IEnumerable{T}"/> of <see cref="Export"/> objects that match /// the conditions defined by <see cref="ImportDefinition"/>, if found; otherwise, an /// empty <see cref="IEnumerable{T}"/>. /// </result> /// <remarks> /// <note type="inheritinfo"> /// The implementers should not treat the cardinality-related mismatches as errors, and are not /// expected to throw exceptions in those cases. /// For instance, if the import requests exactly one export and the provider has no matching exports or more than one, /// it should return an empty <see cref="IEnumerable{T}"/> of <see cref="Export"/>. /// </note> /// </remarks> protected override IEnumerable <Export> GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) { this.ThrowIfDisposed(); this.EnsureRunning(); // Use the version of the catalog appropriate to this atomicComposition ComposablePartCatalog currentCatalog = atomicComposition.GetValueAllowNull(this._catalog); #if SILVERLIGHT IPartCreatorImportDefinition partCreatorDefinition = definition as IPartCreatorImportDefinition; bool isPartCreator = false; if (partCreatorDefinition != null) { definition = partCreatorDefinition.ProductImportDefinition; isPartCreator = true; } #endif CreationPolicy importPolicy = definition.GetRequiredCreationPolicy(); List <Export> exports = new List <Export>(); foreach (var partDefinitionAndExportDefinition in currentCatalog.GetExports(definition)) { if (!IsRejected(partDefinitionAndExportDefinition.Item1, atomicComposition)) { #if SILVERLIGHT if (isPartCreator) { exports.Add(new PartCreatorExport(this, partDefinitionAndExportDefinition.Item1, partDefinitionAndExportDefinition.Item2)); } else #endif { exports.Add(CatalogExport.CreateExport(this, partDefinitionAndExportDefinition.Item1, partDefinitionAndExportDefinition.Item2, importPolicy)); } } } return(exports); }
private ImportDefinition TranslateImport(ReflectionImportDefinition reflectionImport, List <LazyMemberInfo> members, List <Lazy <ParameterInfo> > parameters) { bool isExportFactory = false; ContractBasedImportDefinition productImport = reflectionImport; IPartCreatorImportDefinition exportFactoryImportDefinition = reflectionImport as IPartCreatorImportDefinition; if (exportFactoryImportDefinition != null) { productImport = exportFactoryImportDefinition.ProductImportDefinition; isExportFactory = true; } string contractName = Translate(productImport.ContractName); string requiredTypeIdentity = Translate(productImport.RequiredTypeIdentity); IDictionary <string, object> metadata = TranslateImportMetadata(productImport); ReflectionMemberImportDefinition memberImport = reflectionImport as ReflectionMemberImportDefinition; ImportDefinition import = null; if (memberImport != null) { LazyMemberInfo lazyMember = memberImport.ImportingLazyMember; LazyMemberInfo importingMember = new LazyMemberInfo(lazyMember.MemberType, () => GetAccessors(lazyMember)); if (isExportFactory) { import = new PartCreatorMemberImportDefinition( importingMember, ((ICompositionElement)memberImport).Origin, new ContractBasedImportDefinition( contractName, requiredTypeIdentity, productImport.RequiredMetadata, productImport.Cardinality, productImport.IsRecomposable, false, CreationPolicy.NonShared, metadata)); } else { import = new ReflectionMemberImportDefinition( importingMember, contractName, requiredTypeIdentity, productImport.RequiredMetadata, productImport.Cardinality, productImport.IsRecomposable, false, productImport.RequiredCreationPolicy, metadata, ((ICompositionElement)memberImport).Origin); } members.Add(lazyMember); } else { ReflectionParameterImportDefinition parameterImport = reflectionImport as ReflectionParameterImportDefinition; if (parameterImport == null) { throw new Exception(SR.Diagnostic_InternalExceptionMessage); } Lazy <ParameterInfo> lazyParameter = parameterImport.ImportingLazyParameter; Lazy <ParameterInfo> parameter = new Lazy <ParameterInfo>(() => GetParameter(lazyParameter)); if (isExportFactory) { import = new PartCreatorParameterImportDefinition( parameter, ((ICompositionElement)parameterImport).Origin, new ContractBasedImportDefinition( contractName, requiredTypeIdentity, productImport.RequiredMetadata, productImport.Cardinality, false, true, CreationPolicy.NonShared, metadata)); } else { import = new ReflectionParameterImportDefinition( parameter, contractName, requiredTypeIdentity, productImport.RequiredMetadata, productImport.Cardinality, productImport.RequiredCreationPolicy, metadata, ((ICompositionElement)parameterImport).Origin); } parameters.Add(lazyParameter); } return(import); }