public static bool IsNonSharedInstanceRequired(ImportDefinition importDefinition)
        {
            Requires.NotNull(importDefinition, nameof(importDefinition));

            return(importDefinition.ExportConstraints.Contains(NonSharedPartRequired));
        }
Ejemplo n.º 2
0
        private bool TryCreateImportDefinition(Type importingType, ICustomAttributeProvider member, ImmutableHashSet <IImportSatisfiabilityConstraint> importConstraints, out ImportDefinition importDefinition)
        {
            Requires.NotNull(importingType, nameof(importingType));
            Requires.NotNull(member, nameof(member));

            var importAttribute     = member.GetFirstAttribute <ImportAttribute>();
            var importManyAttribute = member.GetFirstAttribute <ImportManyAttribute>();

            // Importing constructors get implied attributes on their parameters.
            if (importAttribute == null && importManyAttribute == null && member is ParameterInfo)
            {
                importAttribute = new ImportAttribute();
            }

            var sharingBoundaries        = ImmutableHashSet.Create <string>();
            var sharingBoundaryAttribute = member.GetFirstAttribute <SharingBoundaryAttribute>();

            if (sharingBoundaryAttribute != null)
            {
                Verify.Operation(importingType.IsExportFactoryTypeV2(), Strings.IsExpectedOnlyOnImportsOfExportFactoryOfT, typeof(SharingBoundaryAttribute).Name);
                sharingBoundaries = sharingBoundaries.Union(sharingBoundaryAttribute.SharingBoundaryNames);
            }

            if (importAttribute != null)
            {
                Type contractType = GetTypeIdentityFromImportingType(importingType, importMany: false);
                if (contractType.IsAnyLazyType() || contractType.IsExportFactoryTypeV2())
                {
                    contractType = contractType.GetTypeInfo().GetGenericArguments()[0];
                }

                importConstraints = importConstraints
                                    .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),
                    importConstraints,
                    sharingBoundaries);
                return(true);
            }
            else if (importManyAttribute != null)
            {
                Type contractType = GetTypeIdentityFromImportingType(importingType, importMany: true);
                importConstraints = importConstraints
                                    .Union(this.GetMetadataViewConstraints(importingType, importMany: true))
                                    .Union(GetExportTypeIdentityConstraints(contractType));
                importDefinition = new ImportDefinition(
                    string.IsNullOrEmpty(importManyAttribute.ContractName) ? GetContractName(contractType) : importManyAttribute.ContractName,
                    ImportCardinality.ZeroOrMore,
                    GetImportMetadataForGenericTypeImport(contractType),
                    importConstraints,
                    sharingBoundaries);
                return(true);
            }
            else
            {
                importDefinition = null;
                return(false);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Throws <see cref="NotImplementedException"/>.
 /// </summary>
 private protected sealed override IEnumerable <ExportInfo> GetExportsCore(ImportDefinition importDefinition)
 {
     // This should never be called, because our GetExports override calls the inner one instead,
     // which IS implemented.
     throw new NotImplementedException();
 }
Ejemplo n.º 4
0
        private bool TryCreateImportDefinition(Type importingType, ICustomAttributeProvider member, 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);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Forwards the exports query to the inner <see cref="ExportProvider"/>.
 /// </summary>
 /// <param name="importDefinition">A description of the exports desired.</param>
 /// <returns>The resulting exports.</returns>
 public override IEnumerable <Export> GetExports(ImportDefinition importDefinition)
 {
     return(this.inner.GetExports(importDefinition));
 }