private static ImportDefinition WrapImportDefinition(MefV1.Primitives.ImportDefinition definition) { Requires.NotNull(definition, nameof(definition)); var contractImportDefinition = definition as MefV1.Primitives.ContractBasedImportDefinition; var constraints = ImmutableHashSet <IImportSatisfiabilityConstraint> .Empty .Add(new ImportConstraint(definition)); if (contractImportDefinition != null) { constraints = constraints.Union(PartCreationPolicyConstraint.GetRequiredCreationPolicyConstraints(WrapCreationPolicy(contractImportDefinition.RequiredCreationPolicy))); } // Do NOT propagate the cardinality otherwise the export provider will throw // if the cardinality is not met. But it's not our job to throw, since the caller // is going to aggregate our response with other export providers and finally // be responsible for throwing if the ultimate receiver of the result doesn't // get what they expect. // We use ZeroOrMore to indicate we'll accept any response. var cardinality = ImportCardinality.ZeroOrMore; var metadata = (IReadOnlyDictionary <string, object>)definition.Metadata; MefV1.Primitives.ImportDefinition productImportDefinitionV1 = GetExportFactoryProductImportDefinitionIfApplicable(definition); if (productImportDefinitionV1 != null) { var productImportDefinitionV3 = WrapImportDefinition(productImportDefinitionV1); metadata = metadata.ToImmutableDictionary() .Add(CompositionConstants.ExportFactoryProductImportDefinition, productImportDefinitionV3) .Add(CompositionConstants.ExportFactoryTypeMetadataName, ExportFactoryV1Type); } return(new ImportDefinition(definition.ContractName, cardinality, metadata, constraints)); }
private bool TryCreateImportDefinition(Type importingType, ICustomAttributeProvider member, [NotNullWhen(true)] out ImportDefinition?importDefinition) { Requires.NotNull(importingType, nameof(importingType)); Requires.NotNull(member, nameof(member)); ImportAttribute importAttribute = member.GetFirstAttribute <ImportAttribute>(); ImportManyAttribute importManyAttribute = member.GetFirstAttribute <ImportManyAttribute>(); // Importing constructors get implied attributes on their parameters. if (importAttribute == null && importManyAttribute == null && member is ParameterInfo) { importAttribute = new ImportAttribute(); } if (importAttribute != null) { this.ThrowOnInvalidImportingMemberOrParameter(member, isImportMany: false); if (importAttribute.Source != ImportSource.Any) { throw new NotSupportedException(Strings.CustomImportSourceNotSupported); } var requiredCreationPolicy = importingType.IsExportFactoryTypeV1() ? CreationPolicy.NonShared : (CreationPolicy)importAttribute.RequiredCreationPolicy; Type contractType = importAttribute.ContractType ?? GetTypeIdentityFromImportingType(importingType, importMany: false); var constraints = PartCreationPolicyConstraint.GetRequiredCreationPolicyConstraints(requiredCreationPolicy) .Union(this.GetMetadataViewConstraints(importingType, importMany: false)) .Union(GetExportTypeIdentityConstraints(contractType)); importDefinition = new ImportDefinition( string.IsNullOrEmpty(importAttribute.ContractName) ? GetContractName(contractType) : importAttribute.ContractName, importAttribute.AllowDefault ? ImportCardinality.OneOrZero : ImportCardinality.ExactlyOne, GetImportMetadataForGenericTypeImport(contractType), constraints); return(true); } else if (importManyAttribute != null) { this.ThrowOnInvalidImportingMemberOrParameter(member, isImportMany: true); if (importManyAttribute.Source != ImportSource.Any) { throw new NotSupportedException(Strings.CustomImportSourceNotSupported); } var requiredCreationPolicy = GetElementTypeFromMany(importingType).IsExportFactoryTypeV1() ? CreationPolicy.NonShared : (CreationPolicy)importManyAttribute.RequiredCreationPolicy; Type contractType = importManyAttribute.ContractType ?? GetTypeIdentityFromImportingType(importingType, importMany: true); var constraints = PartCreationPolicyConstraint.GetRequiredCreationPolicyConstraints(requiredCreationPolicy) .Union(this.GetMetadataViewConstraints(importingType, importMany: true)) .Union(GetExportTypeIdentityConstraints(contractType)); importDefinition = new ImportDefinition( string.IsNullOrEmpty(importManyAttribute.ContractName) ? GetContractName(contractType) : importManyAttribute.ContractName, ImportCardinality.ZeroOrMore, GetImportMetadataForGenericTypeImport(contractType), constraints); return(true); } else { importDefinition = null; return(false); } }