private static bool ShouldUseSharedPart(CreationPolicy partPolicy, CreationPolicy importPolicy)
            {
                // Matrix that details which policy to use for a given part to satisfy a given import.
                //                   Part.Any   Part.Shared  Part.NonShared
                // Import.Any        Shared     Shared       NonShared
                // Import.Shared     Shared     Shared       N/A
                // Import.NonShared  NonShared  N/A          NonShared

                switch (partPolicy)
                {
                    case CreationPolicy.Any:
                        {
                            if (importPolicy == CreationPolicy.Any ||
                                importPolicy == CreationPolicy.Shared)
                            {
                                return true;
                            }
                            return false;
                        }

                    case CreationPolicy.NonShared:
                        {
                            Assumes.IsTrue(importPolicy != CreationPolicy.Shared);
                            return false;
                        }

                    default:
                        {
                            Assumes.IsTrue(partPolicy == CreationPolicy.Shared);
                            Assumes.IsTrue(importPolicy != CreationPolicy.NonShared);
                            return true;
                        }
                }
            }
Exemplo n.º 2
0
        public static Expression<Func<ExportDefinition, bool>> CreateConstraint(string contractName, string requiredTypeIdentity, IEnumerable<KeyValuePair<string, Type>> requiredMetadata, CreationPolicy requiredCreationPolicy)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(ExportDefinition), "exportDefinition");

            Expression constraintBody = ConstraintServices.CreateContractConstraintBody(contractName, parameter);

            if (!string.IsNullOrEmpty(requiredTypeIdentity))
            {
                Expression typeIdentityConstraintBody = ConstraintServices.CreateTypeIdentityContraint(requiredTypeIdentity, parameter);

                constraintBody = Expression.AndAlso(constraintBody, typeIdentityConstraintBody);
            }

            if (requiredMetadata != null)
            {
                Expression metadataConstraintBody = ConstraintServices.CreateMetadataConstraintBody(requiredMetadata, parameter);
                if (metadataConstraintBody != null)
                {
                    constraintBody = Expression.AndAlso(constraintBody, metadataConstraintBody);
                }
            }

            if (requiredCreationPolicy != CreationPolicy.Any)
            {
                Expression policyConstraintBody = ConstraintServices.CreateCreationPolicyContraint(requiredCreationPolicy, parameter);

                constraintBody = Expression.AndAlso(constraintBody, policyConstraintBody);
            }

            Expression<Func<ExportDefinition, bool>> constraint = Expression.Lambda<Func<ExportDefinition, bool>>(constraintBody, parameter);
            return constraint;
        }
 public IronPythonContractBasedImportDefinition(string methodName, string contractName,
                                                string requiredTypeIdentity,
                                                IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
                                                ImportCardinality cardinality, bool isRecomposable,
                                                bool isPrerequisite, CreationPolicy requiredCreationPolicy)
     : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite,
         requiredCreationPolicy)
 {
     MethodName = methodName;
 }
 public IronPythonContractBasedImportDefinition(string methodName, string contractName,
                                                string requiredTypeIdentity,
                                                IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
                                                ImportCardinality cardinality, bool isRecomposable,
                                                bool isPrerequisite, CreationPolicy requiredCreationPolicy)
     : base(
         contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite,
         requiredCreationPolicy)
 {
     MethodName = methodName;
 }
Exemplo n.º 5
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     Lazy <ParameterInfo> parameter,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
     ImportCardinality cardinality,
     CreationPolicy requiredCreationPolicy,
     ICompositionElement origin)
 {
     return(CreateImportDefinition(parameter, contractName, requiredTypeIdentity, requiredMetadata, cardinality, requiredCreationPolicy, MetadataServices.EmptyMetadata, false, origin));
 }
Exemplo n.º 6
0
 public ConstraintInstance(
     string contractName,
     string requiredTypeIdentity,
     IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
     CreationPolicy requiredCreationPolicy)
     : this(requiredMetadata)
 {
     _contractName = contractName;
     _requiredTypeIdentity = requiredTypeIdentity;
     _requiredCreationPolicy = requiredCreationPolicy;
 }
Exemplo n.º 7
0
 internal PartBuilder(Predicate <Type> selectType)
 {
     SelectType                 = selectType;
     _setCreationPolicy         = false;
     _creationPolicy            = CreationPolicy.Any;
     _typeExportBuilders        = new List <ExportBuilder>();
     _constructorImportBuilders = new List <ImportBuilder>();
     _propertyExports           = new List <Tuple <Predicate <PropertyInfo>, Action <PropertyInfo, ExportBuilder>, Type> >();
     _propertyImports           = new List <Tuple <Predicate <PropertyInfo>, Action <PropertyInfo, ImportBuilder>, Type> >();
     _interfaceExports          = new List <Tuple <Predicate <Type>, Action <Type, ExportBuilder> > >();
 }
        private bool MatchRequiredMatadata(ExportDefinition definition)
        {
            if (!string.IsNullOrEmpty(this.RequiredTypeIdentity))
            {
                string exportTypeIdentity = definition.Metadata.GetValue <string>(CompositionConstants.ExportTypeIdentityMetadataName);

                if (!StringComparers.ContractName.Equals(this.RequiredTypeIdentity, exportTypeIdentity))
                {
                    return(false);
                }
            }

            foreach (KeyValuePair <string, Type> metadataItem in this.RequiredMetadata)
            {
                string metadataKey       = metadataItem.Key;
                Type   metadataValueType = metadataItem.Value;

                object metadataValue = null;
                if (!definition.Metadata.TryGetValue(metadataKey, out metadataValue))
                {
                    return(false);
                }

                if (metadataValue != null)
                {
                    // the metadata value is not null, we can rely on IsInstanceOfType to do the right thing
                    if (!metadataValueType.IsInstanceOfType(metadataValue))
                    {
                        return(false);
                    }
                }
                else
                {
                    // this is an unfortunate special case - typeof(object).IsInstanceofType(null) == false
                    // basically nulls are not considered valid values for anything
                    // We want them to match anything that is a reference type
                    if (metadataValueType.IsValueType)
                    {
                        // this is a pretty expensive check, but we only invoke it when metadata values are null, which is very rare
                        return(false);
                    }
                }
            }

            if (this.RequiredCreationPolicy == CreationPolicy.Any)
            {
                return(true);
            }

            CreationPolicy exportPolicy = definition.Metadata.GetValue <CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName);

            return(exportPolicy == CreationPolicy.Any ||
                   exportPolicy == this.RequiredCreationPolicy);
        }
Exemplo n.º 9
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     LazyMemberInfo importingMember,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy,
     ICompositionElement origin)
 {
     return CreateImportDefinition(importingMember, contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, requiredCreationPolicy, false, origin);
 }
Exemplo n.º 10
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     LazyMemberInfo importingMember,
     string contractName,
     string?requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> >?requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy,
     ICompositionElement?origin)
 {
     return(CreateImportDefinition(importingMember, contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, requiredCreationPolicy, MetadataServices.EmptyMetadata, false, origin));
 }
Exemplo n.º 11
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     Lazy<ParameterInfo> parameter,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
     ImportCardinality cardinality,
     CreationPolicy requiredCreationPolicy,
     bool isExportFactory,
     ICompositionElement origin)
 {
     return ReflectionModelServices.CreateImportDefinition(parameter, contractName, requiredTypeIdentity, requiredMetadata, cardinality, requiredCreationPolicy, isExportFactory, origin);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Gets a dictionary of metadata to include in an <see cref="ExportDefinition"/> to signify the exporting part's CreationPolicy.
        /// </summary>
        /// <param name="partCreationPolicy">The <see cref="CreationPolicy"/> of the exporting <see cref="ComposablePartDefinition"/>.</param>
        /// <returns>A dictionary of metadata.</returns>
        public static ImmutableDictionary <string, object?> GetExportMetadata(CreationPolicy partCreationPolicy)
        {
            var result = ImmutableDictionary.Create <string, object?>();

            // As an optimization, only specify the export metadata if the policy isn't Any.
            // This matches our logic in IsSatisfiedBy that interprets no metadata as no part creation policy.
            if (partCreationPolicy != CreationPolicy.Any)
            {
                result = result.Add(CompositionConstants.PartCreationPolicyMetadataName, partCreationPolicy);
            }

            return(result);
        }
 public ReflectionImportDefinition(
     string contractName, 
     string requiredTypeIdentity,
     IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
     ImportCardinality cardinality, 
     bool isRecomposable, 
     bool isPrerequisite, 
     CreationPolicy requiredCreationPolicy,
     ICompositionElement origin) 
     : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy)
 {
     this._origin = origin;
 }
Exemplo n.º 14
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     Lazy <ParameterInfo> parameter,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
     ImportCardinality cardinality,
     CreationPolicy requiredCreationPolicy,
     IDictionary <string, object> metadata,
     bool isExportFactory,
     ICompositionElement origin)
 {
     return(ReflectionModelServices.CreateImportDefinition(parameter, contractName, requiredTypeIdentity, requiredMetadata, cardinality, requiredCreationPolicy, metadata, isExportFactory, origin));
 }
Exemplo n.º 15
0
 public ReflectionImportDefinition(
     string contractName,
     string requiredTypeIdentity,
     IEnumerable <string> requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     bool isPrerequisite,
     CreationPolicy requiredCreationPolicy,
     ICompositionElement origin)
     : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy)
 {
     this._origin = origin;
 }
Exemplo n.º 16
0
 public static ContractBasedImportDefinition CreateImportDefinition(
     LazyMemberInfo importingMember,
     string contractName,
     string requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy,
     bool isExportFactory,
     ICompositionElement origin)
 {
     return(ReflectionModelServices.CreateImportDefinition(importingMember, contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, requiredCreationPolicy, isExportFactory, origin));
 }
Exemplo n.º 17
0
 public ReflectionImportDefinition(
     string contractName,
     string requiredTypeIdentity,
     IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     bool isPrerequisite,
     CreationPolicy requiredCreationPolicy,
     IDictionary <string, object> metadata,
     ICompositionElement origin)
     : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy, metadata)
 {
     _origin = origin;
 }
        public ReflectionParameterImportDefinition(
            Lazy <ParameterInfo> importingLazyParameter,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <string> requiredMetadata,
            ImportCardinality cardinality,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, false, true, requiredCreationPolicy, origin)
        {
            Assumes.NotNull(importingLazyParameter);

            this._importingLazyParameter = importingLazyParameter;
        }
        public ReflectionParameterImportDefinition(
            Lazy<ParameterInfo> importingLazyParameter,
            string contractName, 
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string,Type>> requiredMetadata,
            ImportCardinality cardinality, 
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin) 
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, false, true, requiredCreationPolicy, origin)
        {
            Assumes.NotNull(importingLazyParameter);

            this._importingLazyParameter = importingLazyParameter;
        }
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<string> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, false, requiredCreationPolicy, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
Exemplo n.º 21
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ContractBasedImportDefinition"/> class
        ///     with the specified contract name, required metadataq, cardinality, value indicating
        ///     if the import definition is recomposable and a value indicating if the import definition
        ///     is a prerequisite.
        /// </summary>
        /// <param name="contractName">
        ///     A <see cref="string"/> containing the contract name of the
        ///     <see cref="Export"/> required by the <see cref="ContractBasedImportDefinition"/>.
        /// </param>
        /// <param name="requiredTypeIdentity">
        ///     The type identity of the export type expected. Use <see cref="AttributedModelServices.GetTypeIdentity(Type)"/>
        ///     to generate a type identity for a given type. If no specific type is required pass <see langword="null"/>.
        /// </param>
        /// <param name="requiredMetadata">
        ///     An <see cref="IEnumerable{T}"/> of <see cref="string"/> objects containing
        ///     the metadata names of the <see cref="Export"/> required by the
        ///     <see cref="ContractBasedImportDefinition"/>; or <see langword="null"/> to
        ///     set the <see cref="RequiredMetadata"/> property to an empty <see cref="IEnumerable{T}"/>.
        /// </param>
        /// <param name="cardinality">
        ///     One of the <see cref="ImportCardinality"/> values indicating the
        ///     cardinality of the <see cref="Export"/> objects required by the
        ///     <see cref="ContractBasedImportDefinition"/>.
        /// </param>
        /// <param name="isRecomposable">
        ///     <see langword="true"/> if the <see cref="ContractBasedImportDefinition"/> can be satisfied
        ///     multiple times throughout the lifetime of a <see cref="ComposablePart"/>, otherwise,
        ///     <see langword="false"/>.
        /// </param>
        /// <param name="isPrerequisite">
        ///     <see langword="true"/> if the <see cref="ContractBasedImportDefinition"/> is required to be
        ///     satisfied before a <see cref="ComposablePart"/> can start producing exported
        ///     objects; otherwise, <see langword="false"/>.
        /// </param>
        /// <param name="requiredCreationPolicy">
        ///     A value indicating that the importer requires a specific <see cref="CreationPolicy"/> for
        ///     the exports used to satisfy this import. If no specific <see cref="CreationPolicy"/> is needed
        ///     pass the default <see cref="CreationPolicy.Any"/>.
        /// </param>
        /// <param name="metadata">The metadata associated with this import.</param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="contractName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="contractName"/> is an empty string ("").
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="requiredMetadata"/> contains an element that is <see langword="null"/>.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="cardinality"/> is not one of the <see cref="ImportCardinality"/>
        ///     values.
        /// </exception>
        public ContractBasedImportDefinition(string contractName, string?requiredTypeIdentity, IEnumerable <KeyValuePair <string, Type> >?requiredMetadata,
                                             ImportCardinality cardinality, bool isRecomposable, bool isPrerequisite, CreationPolicy requiredCreationPolicy, IDictionary <string, object?> metadata)
            : base(contractName, cardinality, isRecomposable, isPrerequisite, metadata)
        {
            Requires.NotNullOrEmpty(contractName, nameof(contractName));

            _requiredTypeIdentity = requiredTypeIdentity;

            if (requiredMetadata != null)
            {
                _requiredMetadata = requiredMetadata;
            }

            _requiredCreationPolicy = requiredCreationPolicy;
        }
Exemplo n.º 22
0
 /// <summary>
 /// Creates a new instance of the <see cref="ConstructorBasedImportDefinition"/> class
 /// based on the given <see cref="ParameterInfo"/>.
 /// </summary>
 /// <param name="contractName">The contract name that is used to identify the current import.</param>
 /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param>
 /// <param name="cardinality">
 ///     One of the enumeration values that indicates the cardinality of the export object required by the import definition.
 /// </param>
 /// <param name="creationPolicy">
 ///     A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import.
 /// </param>
 /// <param name="parameter">The method for which the current object stores the serialized data.</param>
 /// <returns>The serialized definition for the given parameter.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="parameter"/> is <see langword="null" />.
 /// </exception>
 public static ConstructorBasedImportDefinition CreateDefinition(
     string contractName,
     TypeIdentity requiredTypeIdentity,
     ImportCardinality cardinality,
     CreationPolicy creationPolicy,
     ParameterInfo parameter)
 {
     return(CreateDefinition(
                contractName,
                requiredTypeIdentity,
                cardinality,
                creationPolicy,
                parameter,
                t => TypeIdentity.CreateDefinition(t)));
 }
            public static CatalogExport CreateExport(CatalogExportProvider catalogExportProvider,
                ComposablePartDefinition partDefinition, ExportDefinition definition, CreationPolicy importCreationPolicy)
            {
                CreationPolicy partPolicy = partDefinition.Metadata.GetValue<CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName);
                bool isSharedPart = ShouldUseSharedPart(partPolicy, importCreationPolicy);

                if (isSharedPart)
                {
                    return new CatalogExport(catalogExportProvider, partDefinition, definition);
                }
                else
                {
                    return new NonSharedCatalogExport(catalogExportProvider, partDefinition, definition);
                }
            }
        public ContractBasedImportDefinition(string contractName, string requiredTypeIdentity, IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
                                             ImportCardinality cardinality, bool isRecomposable, bool isPrerequisite, CreationPolicy requiredCreationPolicy)
            : base(contractName, cardinality, isRecomposable, isPrerequisite)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");

            this._requiredTypeIdentity = requiredTypeIdentity;

            if (requiredMetadata != null)
            {
                this._requiredMetadata = requiredMetadata;
            }

            this._requiredCreationPolicy = requiredCreationPolicy;
        }
Exemplo n.º 25
0
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <string> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, false, requiredCreationPolicy, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
        private static Expression CreateCreationPolicyContraint(CreationPolicy policy, ParameterExpression parameter)
        {
            Assumes.IsTrue(policy != CreationPolicy.Any);
            Assumes.NotNull(parameter);

            //    !definition.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName) ||
            //        CreationPolicy.Any.Equals(definition.Metadata[CompositionConstants.PartCreationPolicyMetadataName]) ||
            //        policy.Equals(definition.Metadata[CompositionConstants.PartCreationPolicyMetadataName]);

            return(Expression.MakeBinary(ExpressionType.OrElse,
                                         Expression.MakeBinary(ExpressionType.OrElse,
                                                               Expression.Not(CreateMetadataContainsKeyExpression(parameter, CompositionConstants.PartCreationPolicyMetadataName)),
                                                               CreateMetadataValueEqualsExpression(parameter, CreationPolicy.Any, CompositionConstants.PartCreationPolicyMetadataName)),
                                         CreateMetadataValueEqualsExpression(parameter, policy, CompositionConstants.PartCreationPolicyMetadataName)));
        }
Exemplo n.º 27
0
        public ReflectionParameterImportDefinition(
            Lazy <ParameterInfo> importingLazyParameter,
            string contractName,
            string?requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> >?requiredMetadata,
            ImportCardinality cardinality,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object?> metadata,
            ICompositionElement?origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, false, true, requiredCreationPolicy, metadata, origin)
        {
            ArgumentNullException.ThrowIfNull(importingLazyParameter);

            _importingLazyParameter = importingLazyParameter;
        }
Exemplo n.º 28
0
            public static CatalogExport CreateExport(CatalogExportProvider catalogExportProvider,
                                                     ComposablePartDefinition partDefinition, ExportDefinition definition, CreationPolicy importCreationPolicy)
            {
                CreationPolicy partPolicy   = partDefinition.Metadata.GetValue <CreationPolicy>(CompositionConstants.PartCreationPolicyMetadataName);
                bool           isSharedPart = ShouldUseSharedPart(partPolicy, importCreationPolicy);

                if (isSharedPart)
                {
                    return(new CatalogExport(catalogExportProvider, partDefinition, definition));
                }
                else
                {
                    return(new NonSharedCatalogExport(catalogExportProvider, partDefinition, definition));
                }
            }
Exemplo n.º 29
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            bool isPreRequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object> metadata,
            bool isExportFactory,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);
            Contract.Ensures(Contract.Result <ContractBasedImportDefinition>() != null);

            if (isExportFactory)
            {
                return(new PartCreatorMemberImportDefinition(
                           importingMember,
                           origin,
                           new ContractBasedImportDefinition(
                               contractName,
                               requiredTypeIdentity,
                               requiredMetadata,
                               cardinality,
                               isRecomposable,
                               isPreRequisite,
                               CreationPolicy.NonShared,
                               metadata)));
            }
            else
            {
                return(new ReflectionMemberImportDefinition(
                           importingMember,
                           contractName,
                           requiredTypeIdentity,
                           requiredMetadata,
                           cardinality,
                           isRecomposable,
                           isPreRequisite,
                           requiredCreationPolicy,
                           metadata,
                           origin));
            }
        }
 public MetadataContractBasedImportDefinition(Type contractType,
                                              string requiredTypeIdentity,
                                              IEnumerable <Tuple <string, object, IEqualityComparer> > requiredMetadata,
                                              ImportCardinality cardinality,
                                              bool isRecomposable,
                                              bool isPrerequisite,
                                              CreationPolicy requiredCreationPolicy)
     : base(AttributedModelServices.GetContractName(contractType),
            requiredTypeIdentity,
            requiredMetadata.Select(t => new KeyValuePair <string, Type>(t.Item1, t.Item2.GetType())),
            cardinality,
            isRecomposable,
            isPrerequisite,
            requiredCreationPolicy)
 {
     this.RequiredMetadataValues = requiredMetadata.ToArray();
 }
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName, 
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality, 
            bool isRecomposable, 
            bool isPrerequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary<string, object> metadata,
            ICompositionElement origin) 
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy, metadata, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
Exemplo n.º 32
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            bool isPartCreator,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NullOrNotNullElements(requiredMetadata, "requiredMetadata");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);

            if (isPartCreator)
            {
#if SILVERLIGHT
                return(new PartCreatorMemberImportDefinition(
                           importingMember,
                           origin,
                           new ContractBasedImportDefinition(
                               contractName,
                               requiredTypeIdentity,
                               requiredMetadata,
                               cardinality,
                               isRecomposable,
                               false,
                               CreationPolicy.NonShared)));
#else
                throw new ArgumentException("PartCreator is only support in Silverlight version of MEF", "isPartCreator");
#endif
            }
            else
            {
                return(new ReflectionMemberImportDefinition(
                           importingMember,
                           contractName,
                           requiredTypeIdentity,
                           requiredMetadata,
                           cardinality,
                           isRecomposable,
                           requiredCreationPolicy,
                           origin));
            }
        }
Exemplo n.º 33
0
            private static MefV1.CreationPolicy UnwrapCreationPolicy(CreationPolicy creationPolicy)
            {
                switch (creationPolicy)
                {
                case CreationPolicy.Any:
                    return(MefV1.CreationPolicy.Any);

                case CreationPolicy.Shared:
                    return(MefV1.CreationPolicy.Shared);

                case CreationPolicy.NonShared:
                    return(MefV1.CreationPolicy.NonShared);

                default:
                    throw new ArgumentException();
                }
            }
Exemplo n.º 34
0
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            bool isPrerequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object> metadata,
            ICompositionElement origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy, metadata, origin)
        {
            Assumes.NotNull(contractName);

            this._importingLazyMember = importingLazyMember;
        }
Exemplo n.º 35
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            bool isPartCreator,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NullOrNotNullElements(requiredMetadata, "requiredMetadata");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);

            if (isPartCreator)
            {
            #if SILVERLIGHT
                return new PartCreatorMemberImportDefinition(
                    importingMember,
                    origin,
                    new ContractBasedImportDefinition(
                        contractName,
                        requiredTypeIdentity,
                        requiredMetadata,
                        cardinality,
                        isRecomposable,
                        false,
                        CreationPolicy.NonShared));
            #else
                throw new ArgumentException("PartCreator is only support in Silverlight version of MEF", "isPartCreator");
            #endif
            }
            else
            {
                return new ReflectionMemberImportDefinition(
                    importingMember,
                    contractName,
                    requiredTypeIdentity,
                    requiredMetadata,
                    cardinality,
                    isRecomposable,
                    requiredCreationPolicy,
                    origin);
            }
        }
 public MetadataContractBasedImportDefinition(Type contractType,
                                              string requiredTypeIdentity,
                                              IEnumerable<Tuple<string, object, IEqualityComparer>> requiredMetadata,
                                              ImportCardinality cardinality,
                                              bool isRecomposable,
                                              bool isPrerequisite,
                                              CreationPolicy requiredCreationPolicy)
     : base(AttributedModelServices.GetContractName(contractType),
            requiredTypeIdentity,
            requiredMetadata.Select(t => new KeyValuePair<string, Type>(t.Item1, t.Item2.GetType())),
            cardinality,
            isRecomposable,
            isPrerequisite,
            requiredCreationPolicy)
 {
     this.RequiredMetadataValues = requiredMetadata.ToArray();
 }
 public static PropertyBasedImportDefinition CreateDefinition(
     string contractName,
     TypeIdentity requiredTypeIdentity,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy creationPolicy,
     PropertyInfo property)
 {
     return(CreateDefinition(
                contractName,
                requiredTypeIdentity,
                cardinality,
                isRecomposable,
                creationPolicy,
                property,
                t => TypeIdentity.CreateDefinition(t)));
 }
Exemplo n.º 38
0
        /// <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);
        }
Exemplo n.º 39
0
        private static Expression CreateCreationPolicyContraint(CreationPolicy policy, ParameterExpression parameter)
        {
            ArgumentNullException.ThrowIfNull(parameter);

            if (policy == CreationPolicy.Any)
            {
                throw new Exception(SR.Diagnostic_InternalExceptionMessage);
            }

            //    !definition.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName) ||
            //        CreationPolicy.Any.Equals(definition.Metadata[CompositionConstants.PartCreationPolicyMetadataName]) ||
            //        policy.Equals(definition.Metadata[CompositionConstants.PartCreationPolicyMetadataName]);

            return(Expression.MakeBinary(ExpressionType.OrElse,
                                         Expression.MakeBinary(ExpressionType.OrElse,
                                                               Expression.Not(CreateMetadataContainsKeyExpression(parameter, CompositionConstants.PartCreationPolicyMetadataName)),
                                                               CreateMetadataValueEqualsExpression(parameter, CreationPolicy.Any, CompositionConstants.PartCreationPolicyMetadataName)),
                                         CreateMetadataValueEqualsExpression(parameter, policy, CompositionConstants.PartCreationPolicyMetadataName)));
        }
Exemplo n.º 40
0
 public MetadataContract(string contractName,
                         IEnumerable <KeyValuePair <string, Type> > metadata,
                         List <Expression <Func <TMetadata, TMetadata, bool> > > constraints,
                         Func <TMetadata, ExportDefinition, bool> constraint,
                         ImportCardinality importCardinality,
                         CreationPolicy creationPolicy,
                         TMetadata contract)
     : base(contractName,
            AttributedModelServices.GetContractName(typeof(T)),
            metadata,
            importCardinality,
            false,
            false,
            creationPolicy)
 {
     this._constraints = constraints.ToArray();
     this._constraint  = constraint;
     this._contract    = contract;
 }
Exemplo n.º 41
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            Lazy <ParameterInfo> parameter,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            ImportCardinality cardinality,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object> metadata,
            bool isExportFactory,
            ICompositionElement origin)
        {
            Requires.NotNull(parameter, "parameter");
            Requires.NotNullOrEmpty(contractName, "contractName");
            Contract.Ensures(Contract.Result <ContractBasedImportDefinition>() != null);

            if (isExportFactory)
            {
                return(new PartCreatorParameterImportDefinition(
                           parameter,
                           origin,
                           new ContractBasedImportDefinition(
                               contractName,
                               requiredTypeIdentity,
                               requiredMetadata,
                               cardinality,
                               false,
                               true,
                               CreationPolicy.NonShared,
                               metadata)));
            }
            else
            {
                return(new ReflectionParameterImportDefinition(
                           parameter,
                           contractName,
                           requiredTypeIdentity,
                           requiredMetadata,
                           cardinality,
                           requiredCreationPolicy,
                           metadata,
                           origin));
            }
        }
        public ReflectionMemberImportDefinition(
            LazyMemberInfo importingLazyMember,
            string contractName,
            string?requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> >?requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            bool isPrerequisite,
            CreationPolicy requiredCreationPolicy,
            IDictionary <string, object?> metadata,
            ICompositionElement?origin)
            : base(contractName, requiredTypeIdentity, requiredMetadata, cardinality, isRecomposable, isPrerequisite, requiredCreationPolicy, metadata, origin)
        {
            if (contractName == null)
            {
                throw new ArgumentNullException(nameof(contractName));
            }

            _importingLazyMember = importingLazyMember;
        }
Exemplo n.º 43
0
        public static Func <ExportDefinition, bool> CreateConstraint(
            string contractName,
            string requiredTypeIdentity,
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata,
            CreationPolicy requiredCreationPolicy)
        {
            var constraintInstance = new ConstraintInstance(contractName, requiredTypeIdentity, requiredMetadata, requiredCreationPolicy);
            var constraint         = constraintInstance.GetExportDefinitionConstraint();

            return(constraint);

            //ParameterExpression parameter = Expression.Parameter(typeof(ExportDefinition), "exportDefinition");

            //Expression constraintBody = ConstraintServices.CreateContractConstraintBody(contractName, parameter);

            //if (!string.IsNullOrEmpty(requiredTypeIdentity))
            //{
            //    Expression typeIdentityConstraintBody = ConstraintServices.CreateTypeIdentityContraint(requiredTypeIdentity, parameter);

            //    constraintBody = Expression.AndAlso(constraintBody, typeIdentityConstraintBody);
            //}

            //if (requiredMetadata != null)
            //{
            //    Expression metadataConstraintBody = ConstraintServices.CreateMetadataConstraintBody(requiredMetadata, parameter);
            //    if (metadataConstraintBody != null)
            //    {
            //        constraintBody = Expression.AndAlso(constraintBody, metadataConstraintBody);
            //    }
            //}

            //if (requiredCreationPolicy != CreationPolicy.Any)
            //{
            //    Expression policyConstraintBody = ConstraintServices.CreateCreationPolicyContraint(requiredCreationPolicy, parameter);

            //    constraintBody = Expression.AndAlso(constraintBody, policyConstraintBody);
            //}

            //Expression<Func<ExportDefinition, bool>> constraint = Expression.Lambda<Func<ExportDefinition, bool>>(constraintBody, parameter);
            //return constraint;
        }
Exemplo n.º 44
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            Lazy<ParameterInfo> parameter,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<string> requiredMetadata,
            ImportCardinality cardinality,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
        {
            Requires.NotNull(parameter, "parameter");
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NotNullOrNullElements(requiredMetadata, "requiredMetadata");

            return new ReflectionParameterImportDefinition(
                parameter,
                contractName,
                requiredTypeIdentity,
                requiredMetadata,
                cardinality,
                requiredCreationPolicy,
                origin);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyBasedImportDefinition"/> class.
        /// </summary>
        /// <param name="contractName">The contract name that is used to identify the current import.</param>
        /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param>
        /// <param name="cardinality">
        ///     One of the enumeration values that indicates the cardinality of the export object required by the import definition.
        /// </param>
        /// <param name="isRecomposable">
        ///     <see langword="true" /> to specify that the import definition can be satisfied multiple times throughout the lifetime of a parts; 
        ///     otherwise, <see langword="false" />.
        /// </param>
        /// <param name="creationPolicy">
        ///     A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import.
        /// </param>
        /// <param name="declaringType">The type that defines the property.</param>
        /// <param name="property">The property for which the current object stores the serialized data.</param>
        private PropertyBasedImportDefinition(
            string contractName,
            TypeIdentity requiredTypeIdentity,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy creationPolicy,
            TypeIdentity declaringType,
            PropertyDefinition property)
            : base(contractName, 
                requiredTypeIdentity, 
                cardinality,
                isRecomposable,
                false,
                creationPolicy,
                declaringType)
        {
            {
                Lokad.Enforce.Argument(() => property);
            }

            m_Property = property;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConstructorBasedImportDefinition"/> class.
        /// </summary>
        /// <param name="contractName">The contract name that is used to identify the current import.</param>
        /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param>
        /// <param name="cardinality">
        ///     One of the enumeration values that indicates the cardinality of the export object required by the import definition.
        /// </param>
        /// <param name="creationPolicy">
        ///     A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import.
        /// </param>
        /// <param name="declaringType">The type that declares the constructor on which the import is placed.</param>
        /// <param name="constructor">The constructor that declares the import.</param>
        /// <param name="parameter">The parameter on which the import is defined.</param>
        private ConstructorBasedImportDefinition(
            string contractName,
            TypeIdentity requiredTypeIdentity,
            ImportCardinality cardinality,
            CreationPolicy creationPolicy,
            TypeIdentity declaringType,
            ConstructorDefinition constructor,
            ParameterDefinition parameter)
            : base(contractName, 
                requiredTypeIdentity, 
                cardinality,
                false,
                true,
                creationPolicy,
                declaringType)
        {
            {
                Lokad.Enforce.Argument(() => parameter);
            }

            m_Constructor = constructor;
            m_Parameter = parameter;
        }
Exemplo n.º 47
0
        public static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<string> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NotNullOrNullElements(requiredMetadata, "requiredMetadata");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);

            return new ReflectionMemberImportDefinition(
                importingMember,
                contractName,
                requiredTypeIdentity,
                requiredMetadata,
                cardinality,
                isRecomposable,
                requiredCreationPolicy,
                origin);
        }
 public CreationPolicyAttribute(CreationPolicy policy)
 {
     this.Policy = policy;
 }
Exemplo n.º 49
0
 public PartCreationPolicyFilter(CreationPolicy creationPolicy)
 {
     CreationPolicy = creationPolicy;
 }
Exemplo n.º 50
0
        public virtual ImportDefinition BuildImportDefinition(ParameterInfo parameterInfo, 
            string contractName, string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality,
            CreationPolicy requiredCreationPolicy)
        {
            // TODO: IsCollection call

            return ReflectionModelServices.CreateImportDefinition(
                new Lazy<ParameterInfo>(() => parameterInfo), 
                contractName, requiredTypeIdentity, 
                requiredMetadata, cardinality, 
                requiredCreationPolicy, null);
        }
Exemplo n.º 51
0
 /// <include file='Composition.xdoc' path='docs/doc[@for="PartCreationPolicyAttribute.ctor"]/*'/>
 public PartCreationPolicyAttribute(CreationPolicy creationPolicy)
 {
     this.CreationPolicy = creationPolicy;
 }
Exemplo n.º 52
0
        internal static ContractBasedImportDefinition CreateImportDefinition(
            LazyMemberInfo importingMember,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality,
            bool isRecomposable,
            CreationPolicy requiredCreationPolicy,
            bool isExportFactory,
            ICompositionElement origin)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.IsInMembertypeSet(importingMember.MemberType, "importingMember", MemberTypes.Property | MemberTypes.Field);
            Contract.Ensures(Contract.Result<ContractBasedImportDefinition>() != null);

            if (isExportFactory)
            {
                return new PartCreatorMemberImportDefinition(
                    importingMember,
                    origin,
                    new ContractBasedImportDefinition(
                        contractName,
                        requiredTypeIdentity,
                        requiredMetadata,
                        cardinality,
                        isRecomposable,
                        false,
                        CreationPolicy.NonShared));
            }
            else
            {
                return new ReflectionMemberImportDefinition(
                    importingMember,
                    contractName,
                    requiredTypeIdentity,
                    requiredMetadata,
                    cardinality,
                    isRecomposable,
                    requiredCreationPolicy,
                    origin);
            }
        }
Exemplo n.º 53
0
        internal static ContractBasedImportDefinition CreateImportDefinition(
            Lazy<ParameterInfo> parameter,
            string contractName,
            string requiredTypeIdentity,
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
            ImportCardinality cardinality,
            CreationPolicy requiredCreationPolicy,
            bool isExportFactory,
            ICompositionElement origin)
        {
            Requires.NotNull(parameter, "parameter");
            Requires.NotNullOrEmpty(contractName, "contractName");
            Contract.Ensures(Contract.Result<ContractBasedImportDefinition>() != null);

            if (isExportFactory)
            {
                return new PartCreatorParameterImportDefinition(
                    parameter,
                    origin,
                    new ContractBasedImportDefinition(
                        contractName,
                        requiredTypeIdentity,
                        requiredMetadata,
                        cardinality,
                        false,
                        true,
                        CreationPolicy.NonShared));
            }
            else
            {
                return new ReflectionParameterImportDefinition(
                    parameter,
                    contractName,
                    requiredTypeIdentity,
                    requiredMetadata,
                    cardinality,
                    requiredCreationPolicy,
                    origin);
            }
        }
Exemplo n.º 54
0
 public Export(Type type, CreationPolicy creationPolicy)
 {
     this._creationPolicy = creationPolicy;
     this._contractType = type;
 }
Exemplo n.º 55
0
        private static Expression CreateCreationPolicyContraint(CreationPolicy policy, ParameterExpression parameter)
        {
            Assumes.IsTrue(policy != CreationPolicy.Any);
            Assumes.NotNull(parameter);

            //    !definition.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName) ||
            //        CreationPolicy.Any.Equals(definition.Metadata[CompositionConstants.PartCreationPolicyMetadataName]) ||
            //        policy.Equals(definition.Metadata[CompositionConstants.PartCreationPolicyMetadataName]);

            return  Expression.MakeBinary(ExpressionType.OrElse,
                        Expression.MakeBinary(ExpressionType.OrElse,
                            Expression.Not(CreateMetadataContainsKeyExpression(parameter, CompositionConstants.PartCreationPolicyMetadataName)),
                            CreateMetadataValueEqualsExpression(parameter, CreationPolicy.Any, CompositionConstants.PartCreationPolicyMetadataName)),
                        CreateMetadataValueEqualsExpression(parameter, policy, CompositionConstants.PartCreationPolicyMetadataName));
        }
 /// <summary>
 /// Creates a new instance of the <see cref="ConstructorBasedImportDefinition"/> class 
 /// based on the given <see cref="ParameterInfo"/>.
 /// </summary>
 /// <param name="contractName">The contract name that is used to identify the current import.</param>
 /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param>
 /// <param name="cardinality">
 ///     One of the enumeration values that indicates the cardinality of the export object required by the import definition.
 /// </param>
 /// <param name="creationPolicy">
 ///     A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import.
 /// </param>
 /// <param name="parameter">The method for which the current object stores the serialized data.</param>
 /// <returns>The serialized definition for the given parameter.</returns>
 /// <exception cref="ArgumentNullException">
 ///     Thrown if <paramref name="parameter"/> is <see langword="null" />.
 /// </exception>
 public static ConstructorBasedImportDefinition CreateDefinition(
     string contractName,
     TypeIdentity requiredTypeIdentity,
     ImportCardinality cardinality,
     CreationPolicy creationPolicy,
     ParameterInfo parameter)
 {
     return CreateDefinition(
         contractName,
         requiredTypeIdentity,
         cardinality,
         creationPolicy,
         parameter,
         t => TypeIdentity.CreateDefinition(t));
 }
        /// <summary>
        /// Creates a new instance of the <see cref="ConstructorBasedImportDefinition"/> class based 
        /// on the given <see cref="ParameterInfo"/>.
        /// </summary>
        /// <param name="contractName">The contract name that is used to identify the current import.</param>
        /// <param name="requiredTypeIdentity">The type identity of the export type expected.</param>
        /// <param name="cardinality">
        ///     One of the enumeration values that indicates the cardinality of the export object required by the import definition.
        /// </param>
        /// <param name="creationPolicy">
        ///     A value that indicates that the importer requires a specific creation policy for the exports used to satisfy this import.
        /// </param>
        /// <param name="parameter">The method for which the current object stores the serialized data.</param>
        /// <param name="identityGenerator">The function that creates type identities.</param>
        /// <returns>The serialized definition for the given parameter.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="parameter"/> is <see langword="null" />.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown if <paramref name="identityGenerator"/> is <see langword="null" />.
        /// </exception>
        public static ConstructorBasedImportDefinition CreateDefinition(
            string contractName,
            TypeIdentity requiredTypeIdentity,
            ImportCardinality cardinality,
            CreationPolicy creationPolicy,
            ParameterInfo parameter,
            Func<Type, TypeIdentity> identityGenerator)
        {
            {
                Lokad.Enforce.Argument(() => parameter);
                Lokad.Enforce.Argument(() => identityGenerator);
            }

            return new ConstructorBasedImportDefinition(
                contractName,
                requiredTypeIdentity,
                cardinality,
                creationPolicy,
                identityGenerator(parameter.Member.DeclaringType),
                ConstructorDefinition.CreateDefinition(parameter.Member as ConstructorInfo, identityGenerator),
                ParameterDefinition.CreateDefinition(parameter, identityGenerator));
        }
Exemplo n.º 58
0
 public virtual ImportDefinition BuildImportDefinition(MemberInfo member,
     string contractName, string requiredTypeIdentity,
     IEnumerable<KeyValuePair<string, Type>> requiredMetadata,
     ImportCardinality cardinality,
     bool isRecomposable,
     CreationPolicy requiredCreationPolicy)
 {
     return ReflectionModelServices.CreateImportDefinition(
         new LazyMemberInfo(member),
         contractName, requiredTypeIdentity,
         requiredMetadata, cardinality,
         isRecomposable, requiredCreationPolicy, null);
 }
Exemplo n.º 59
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ContractBasedImportDefinition"/> class 
        ///     with the specified contract name, required metadataq, cardinality, value indicating 
        ///     if the import definition is recomposable and a value indicating if the import definition 
        ///     is a prerequisite.
        /// </summary>
        /// <param name="contractName">
        ///     A <see cref="String"/> containing the contract name of the 
        ///     <see cref="Export"/> required by the <see cref="ContractBasedImportDefinition"/>.
        /// </param>
        /// <param name="requiredTypeIdentity">
        ///     The type identity of the export type expected. Use <see cref="AttributedModelServices.GetTypeIdentity(Type)"/>
        ///     to generate a type identity for a given type. If no specific type is required pass <see langword="null"/>.
        /// </param>
        /// <param name="requiredMetadata">
        ///     An <see cref="IEnumerable{T}"/> of <see cref="String"/> objects containing
        ///     the metadata names of the <see cref="Export"/> required by the 
        ///     <see cref="ContractBasedImportDefinition"/>; or <see langword="null"/> to
        ///     set the <see cref="RequiredMetadata"/> property to an empty <see cref="IEnumerable{T}"/>.
        /// </param>
        /// <param name="cardinality">
        ///     One of the <see cref="ImportCardinality"/> values indicating the 
        ///     cardinality of the <see cref="Export"/> objects required by the
        ///     <see cref="ContractBasedImportDefinition"/>.
        /// </param>
        /// <param name="isRecomposable">
        ///     <see langword="true"/> if the <see cref="ContractBasedImportDefinition"/> can be satisfied 
        ///     multiple times throughout the lifetime of a <see cref="ComposablePart"/>, otherwise, 
        ///     <see langword="false"/>.
        /// </param>
        /// <param name="isPrerequisite">
        ///     <see langword="true"/> if the <see cref="ContractBasedImportDefinition"/> is required to be 
        ///     satisfied before a <see cref="ComposablePart"/> can start producing exported 
        ///     objects; otherwise, <see langword="false"/>.
        /// </param>
        /// <param name="requiredCreationPolicy">
        ///     A value indicating that the importer requires a specific <see cref="CreationPolicy"/> for 
        ///     the exports used to satisfy this import. If no specific <see cref="CreationPolicy"/> is needed
        ///     pass the default <see cref="CreationPolicy.Any"/>.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="contractName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="contractName"/> is an empty string ("").
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="requiredMetadata"/> contains an element that is <see langword="null"/>.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="cardinality"/> is not one of the <see cref="ImportCardinality"/> 
        ///     values.
        /// </exception>
        public ContractBasedImportDefinition(string contractName, string requiredTypeIdentity, IEnumerable<string> requiredMetadata, 
            ImportCardinality cardinality, bool isRecomposable, bool isPrerequisite, CreationPolicy requiredCreationPolicy)
            : base(contractName, cardinality, isRecomposable, isPrerequisite)
        {
            Requires.NotNullOrEmpty(contractName, "contractName");
            Requires.NullOrNotNullElements(requiredMetadata, "requiredMetadata");

            this._requiredTypeIdentity = requiredTypeIdentity;

            if (requiredMetadata != null)
            {
                this._requiredMetadata = requiredMetadata;
            }

            this._requiredCreationPolicy = requiredCreationPolicy;
        }
Exemplo n.º 60
0
 /// <summary>Creates attribute.</summary> <param name="policy"><see cref="CreationPolicy"/></param>
 public PartCreationPolicyAttribute(CreationPolicy policy)
 {
     CreationPolicy = policy;
 }