public void Constructor_WithNullRequiredMetadata()
        {
            Lazy <ParameterInfo> parameter = CreateLazyParameter();

            ReflectionParameterImportDefinition definition = new ReflectionParameterImportDefinition(
                parameter, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, CreationPolicy.NonShared, null, null);

            Assert.NotNull(definition.RequiredMetadata);
            Assert.Equal(0, definition.RequiredMetadata.Count());
        }
        public void SetDefinition_OriginIsSet()
        {
            Lazy <ParameterInfo> parameter = CreateLazyParameter();
            var expectedPartDefinition     = PartDefinitionFactory.CreateAttributed(typeof(object));

            ReflectionParameterImportDefinition definition = new ReflectionParameterImportDefinition(
                parameter, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, CreationPolicy.NonShared, null, expectedPartDefinition);

            Assert.Same(expectedPartDefinition, ((ICompositionElement)definition).Origin);
        }
        public void SetDefinition_OriginIsSet()
        {
            Lazy<ParameterInfo> parameter = CreateLazyParameter();
            var expectedPartDefinition = PartDefinitionFactory.CreateAttributed(typeof(object));

            ReflectionParameterImportDefinition definition = new ReflectionParameterImportDefinition(
                parameter, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, CreationPolicy.NonShared, expectedPartDefinition);

            Assert.AreSame(expectedPartDefinition, ((ICompositionElement)definition).Origin);
        }
        public void Constructor_WithNullRequiredMetadata()
        {
            Lazy<ParameterInfo> parameter = CreateLazyParameter();

            ReflectionParameterImportDefinition definition = new ReflectionParameterImportDefinition(
                parameter, "Contract", (string)null, null, ImportCardinality.ZeroOrMore, CreationPolicy.NonShared, null);


            Assert.IsNotNull(definition.RequiredMetadata);
            Assert.AreEqual(0, definition.RequiredMetadata.Count());
        }
Пример #5
0
        public static Lazy <ParameterInfo> GetImportingParameter(ImportDefinition importDefinition)
        {
            Requires.NotNull(importDefinition, "importDefinition");

            ReflectionParameterImportDefinition reflectionParameterImportDefinition = importDefinition as ReflectionParameterImportDefinition;

            if (reflectionParameterImportDefinition == null)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Strings.ReflectionModel_InvalidParameterImportDefinition, importDefinition.GetType()),
                          "importDefinition");
            }

            return(reflectionParameterImportDefinition.ImportingLazyParameter);
        }
Пример #6
0
        public static Lazy <ParameterInfo> GetImportingParameter(ImportDefinition importDefinition)
        {
            Requires.NotNull(importDefinition, nameof(importDefinition));
            Contract.Ensures(Contract.Result <Lazy <ParameterInfo> >() != null);

            ReflectionParameterImportDefinition reflectionParameterImportDefinition = importDefinition as ReflectionParameterImportDefinition;

            if (reflectionParameterImportDefinition == null)
            {
                throw new ArgumentException(
                          SR.Format(SR.ReflectionModel_InvalidParameterImportDefinition, importDefinition.GetType()),
                          nameof(importDefinition));
            }

            return(reflectionParameterImportDefinition.ImportingLazyParameter);
        }
        public void Constructor()
        {
            Lazy<ParameterInfo> parameter = CreateLazyParameter();
            IEnumerable<KeyValuePair<string, Type>> requiredMetadata = new KeyValuePair<string, Type>[] { new KeyValuePair<string, Type>("Foo", typeof(object)) };

            ReflectionParameterImportDefinition definition = new ReflectionParameterImportDefinition(
                parameter, "Contract", (string)null, requiredMetadata, ImportCardinality.ZeroOrMore, CreationPolicy.NonShared, null);

            Assert.AreSame(parameter, definition.ImportingLazyParameter);
            Assert.AreEqual("Contract", definition.ContractName);
            Assert.AreSame(requiredMetadata, definition.RequiredMetadata);
            Assert.AreEqual(CreationPolicy.NonShared, definition.RequiredCreationPolicy);
            Assert.AreEqual(false, definition.IsRecomposable);
            Assert.AreEqual(true, definition.IsPrerequisite);
            Assert.IsNull(((ICompositionElement)definition).Origin);
            Assert.IsNotNull(((ICompositionElement)definition).DisplayName);
        }
        public void Constructor()
        {
            Lazy <ParameterInfo> parameter = CreateLazyParameter();
            IEnumerable <KeyValuePair <string, Type> > requiredMetadata = new KeyValuePair <string, Type>[] { new KeyValuePair <string, Type>("Foo", typeof(object)) };

            ReflectionParameterImportDefinition definition = new ReflectionParameterImportDefinition(
                parameter, "Contract", (string)null, requiredMetadata, ImportCardinality.ZeroOrMore, CreationPolicy.NonShared, null);

            Assert.AreSame(parameter, definition.ImportingLazyParameter);
            Assert.AreEqual("Contract", definition.ContractName);
            Assert.AreSame(requiredMetadata, definition.RequiredMetadata);
            Assert.AreEqual(CreationPolicy.NonShared, definition.RequiredCreationPolicy);
            Assert.AreEqual(false, definition.IsRecomposable);
            Assert.AreEqual(true, definition.IsPrerequisite);
            Assert.IsNull(((ICompositionElement)definition).Origin);
            Assert.IsNotNull(((ICompositionElement)definition).DisplayName);
        }
        private static string CreateDisplayNameExpectationFromContractName(ReflectionParameterImportDefinition definition, Type type)
        {
            string contractName = AttributedModelServices.GetContractName(type);

            return(string.Format("{0} (Parameter=\"\", ContractName=\"{1}\")", definition.ImportingLazyParameter.Value.Member.GetDisplayName(), contractName));
        }
 private static string CreateDisplayNameExpectationFromParameterName(ReflectionParameterImportDefinition definition, string name)
 {
     return(string.Format("{0} (Parameter=\"{1}\", ContractName=\"System.String\")", definition.ImportingLazyParameter.Value.Member.GetDisplayName(), name));
 }
        private ImportDefinition TranslateImport(ReflectionImportDefinition reflectionImport, List <LazyMemberInfo> members, List <Lazy <ParameterInfo> > parameters)
        {
            bool isExportFactory = false;
            ContractBasedImportDefinition productImport = reflectionImport;

            if (reflectionImport is IPartCreatorImportDefinition exportFactoryImportDefinition)
            {
                productImport   = exportFactoryImportDefinition.ProductImportDefinition;
                isExportFactory = true;
            }

            string contractName                    = Translate(productImport.ContractName);
            string requiredTypeIdentity            = Translate(productImport.RequiredTypeIdentity !);
            IDictionary <string, object?> metadata = TranslateImportMetadata(productImport);

            ImportDefinition?import = null;

            if (reflectionImport is ReflectionMemberImportDefinition memberImport)
            {
                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);
        }
        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 = this.Translate(productImport.ContractName);
            string requiredTypeIdentity = this.Translate(productImport.RequiredTypeIdentity);
            IDictionary<string, object> metadata = this.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;
                Assumes.NotNull(parameterImport);

                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;
        }
 private static string CreateDisplayNameExpectationFromParameterName(ReflectionParameterImportDefinition definition, string name)
 {
     return String.Format("{0} (Parameter=\"{1}\", ContractName=\"System.String\")", definition.ImportingLazyParameter.Value.Member.GetDisplayName(), name);
 }
        private static string CreateDisplayNameExpectationFromContractName(ReflectionParameterImportDefinition definition, Type type)
        {
            string contractName = AttributedModelServices.GetContractName(type);

            return String.Format("{0} (Parameter=\"\", ContractName=\"{1}\")", definition.ImportingLazyParameter.Value.Member.GetDisplayName(), contractName);
        }