Beispiel #1
0
        public static ReflectionMemberImportDefinition CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
        {
            Requires.NotNull(member, "member");

            ReflectionWritableMember reflectionMember = member.ToReflectionWritableMember();
            IAttributedImport        attributedImport = AttributedModelDiscovery.GetAttributedImport(reflectionMember, member);
            ImportType importType = new ImportType(reflectionMember.ReturnType, attributedImport.Cardinality);

            if (importType.IsPartCreator)
            {
                return(new PartCreatorMemberImportDefinition(
                           new LazyMemberInfo(member),
                           origin,
                           new ContractBasedImportDefinition(
                               attributedImport.GetContractNameFromImport(importType),
                               attributedImport.GetTypeIdentityFromImport(importType),
                               CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                               attributedImport.Cardinality,
                               attributedImport.AllowRecomposition,
                               false,
                               CreationPolicy.NonShared)));
            }
            else
            {
                return(new ReflectionMemberImportDefinition(
                           new LazyMemberInfo(member),
                           attributedImport.GetContractNameFromImport(importType),
                           attributedImport.GetTypeIdentityFromImport(importType),
                           CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                           attributedImport.Cardinality,
                           attributedImport.AllowRecomposition,
                           attributedImport.RequiredCreationPolicy,
                           origin));
            }
        }
Beispiel #2
0
        public static ReflectionParameterImportDefinition CreateParameterImportDefinition(ParameterInfo parameter, ICompositionElement origin)
        {
            Requires.NotNull(parameter, "parameter");

            ReflectionParameter reflectionParameter = parameter.ToReflectionParameter();
            IAttributedImport   attributedImport    = AttributedModelDiscovery.GetAttributedImport(reflectionParameter, parameter);
            ImportType          importType          = new ImportType(reflectionParameter.ReturnType, attributedImport.Cardinality);

            if (importType.IsPartCreator)
            {
                return(new PartCreatorParameterImportDefinition(
                           new Lazy <ParameterInfo>(() => parameter),
                           origin,
                           new ContractBasedImportDefinition(
                               attributedImport.GetContractNameFromImport(importType),
                               attributedImport.GetTypeIdentityFromImport(importType),
                               CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                               attributedImport.Cardinality,
                               false,
                               true,
                               CreationPolicy.NonShared)));
            }
            else
            {
                return(new ReflectionParameterImportDefinition(
                           new Lazy <ParameterInfo>(() => parameter),
                           attributedImport.GetContractNameFromImport(importType),
                           attributedImport.GetTypeIdentityFromImport(importType),
                           CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                           attributedImport.Cardinality,
                           attributedImport.RequiredCreationPolicy,
                           origin));
            }
        }
        private ReflectionComposablePartDefinition CreateDefinition(Type type)
        {
            var definition = AttributedModelDiscovery.CreatePartDefinition(type, null, false, ElementFactory.Create());

            Assert.Equal(type, definition.GetPartType());

            return(definition);
        }
Beispiel #4
0
        public static ReflectionComposablePart CreatePart(object attributedPart)
        {
            Assumes.NotNull(attributedPart);

            // If given an instance then we want to pass the default composition options because we treat it as a shared part
            ReflectionComposablePartDefinition definition = AttributedModelDiscovery.CreatePartDefinition(attributedPart.GetType(), PartCreationPolicyAttribute.Shared, true, (ICompositionElement)null);

            return(new ReflectionComposablePart(definition, attributedPart));
        }
        public static ReflectionComposablePart CreatePart(object attributedPart)
        {
            Assumes.NotNull(attributedPart);

            // If given an instance then we want to pass the default composition options because we treat it as a shared part
            // TODO: ICompositionElement Give this def an origin indicating that it was added directly to the MutableExportProvider.

            ReflectionComposablePartDefinition definition = AttributedModelDiscovery.CreatePartDefinition(attributedPart.GetType(), PartCreationPolicyAttribute.Shared, true, (ICompositionElement)null);

            return(new ReflectionComposablePart(definition, attributedPart));
        }
Beispiel #6
0
        public static ReflectionMemberImportDefinition CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
        {
            Requires.NotNull(member, "member");

            ReflectionWritableMember reflectionMember = member.ToReflectionWritableMember();
            IAttributedImport        attributedImport = AttributedModelDiscovery.GetAttributedImport(reflectionMember, member);
            ImportType importType = new ImportType(reflectionMember.ReturnType, attributedImport.Cardinality);

            if (importType.IsPartCreator)
            {
                return(new PartCreatorMemberImportDefinition(
                           new LazyMemberInfo(member),
                           origin,
                           new ContractBasedImportDefinition(
                               attributedImport.GetContractNameFromImport(importType),
                               attributedImport.GetTypeIdentityFromImport(importType),
                               CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                               attributedImport.Cardinality,
                               attributedImport.AllowRecomposition,
                               false,
                               (attributedImport.RequiredCreationPolicy != CreationPolicy.NewScope) ? CreationPolicy.NonShared : CreationPolicy.NewScope,
                               CompositionServices.GetImportMetadata(importType, attributedImport))));
            }
            else
            {
                // A Standard parameter import is not allowed to be marked as requiring NewScope at this time.
                if (attributedImport.RequiredCreationPolicy == CreationPolicy.NewScope)
                {
                    throw new ComposablePartException(
                              String.Format(CultureInfo.CurrentCulture,
                                            Strings.InvalidPartCreationPolicyOnImport,
                                            attributedImport.RequiredCreationPolicy),
                              origin);
                }

                //Does this Import re-export the value if so, make it a rpe-requisite
                bool isPrerequisite = member.GetAttributes <ExportAttribute>().Length > 0;
                return(new ReflectionMemberImportDefinition(
                           new LazyMemberInfo(member),
                           attributedImport.GetContractNameFromImport(importType),
                           attributedImport.GetTypeIdentityFromImport(importType),
                           CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                           attributedImport.Cardinality,
                           attributedImport.AllowRecomposition,
                           isPrerequisite,
                           attributedImport.RequiredCreationPolicy,
                           CompositionServices.GetImportMetadata(importType, attributedImport),
                           origin));
            }
        }
Beispiel #7
0
        public static ReflectionComposablePart CreatePart(object attributedPart, ReflectionContext reflectionContext)
        {
            Assumes.NotNull(attributedPart);
            Assumes.NotNull(reflectionContext);

            // If given an instance then we want to pass the default composition options because we treat it as a shared part
            var mappedType = reflectionContext.MapType(IntrospectionExtensions.GetTypeInfo(attributedPart.GetType()));

            if (mappedType.Assembly.ReflectionOnly)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.Argument_ReflectionContextReturnsReflectionOnlyType, nameof(reflectionContext)), nameof(reflectionContext));
            }

            ReflectionComposablePartDefinition definition = AttributedModelDiscovery.CreatePartDefinition(mappedType, PartCreationPolicyAttribute.Shared, true, (ICompositionElement)null);

            return(CreatePart(definition, attributedPart));
        }
        public static ReflectionParameterImportDefinition CreateParameterImportDefinition(ParameterInfo parameter, ICompositionElement origin)
        {
            Requires.NotNull(parameter, "parameter");

            ReflectionParameter reflectionParameter = parameter.ToReflectionParameter();

            AttributedImportDefinitionCreationInfo importCreationInfo = AttributedModelDiscovery.GetImportDefinitionCreationInfo(reflectionParameter, parameter);

            return(new ReflectionParameterImportDefinition(
                       parameter.AsLazy(),
                       importCreationInfo.ContractName,
                       importCreationInfo.RequiredTypeIdentity,
                       importCreationInfo.RequiredMetadata,
                       importCreationInfo.Cardinality,
                       importCreationInfo.RequiredCreationPolicy,
                       origin));
        }
Beispiel #9
0
        public static ReflectionParameterImportDefinition CreateParameterImportDefinition(ParameterInfo parameter, ICompositionElement origin)
        {
            Requires.NotNull(parameter, "parameter");

            ReflectionParameter reflectionParameter = parameter.ToReflectionParameter();
            IAttributedImport   attributedImport    = AttributedModelDiscovery.GetAttributedImport(reflectionParameter, parameter);
            ImportType          importType          = new ImportType(reflectionParameter.ReturnType, attributedImport.Cardinality);

            if (importType.IsPartCreator)
            {
                return(new PartCreatorParameterImportDefinition(
                           new Lazy <ParameterInfo>(() => parameter),
                           origin,
                           new ContractBasedImportDefinition(
                               attributedImport.GetContractNameFromImport(importType),
                               attributedImport.GetTypeIdentityFromImport(importType),
                               CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                               attributedImport.Cardinality,
                               false,
                               true,
                               (attributedImport.RequiredCreationPolicy != CreationPolicy.NewScope) ? CreationPolicy.NonShared : CreationPolicy.NewScope,
                               CompositionServices.GetImportMetadata(importType, attributedImport))));
            }
            else
            {
                // A Standard import is not allowed to be marked as requiring NewScope at this time.
                if (attributedImport.RequiredCreationPolicy == CreationPolicy.NewScope)
                {
                    throw new ComposablePartException(
                              String.Format(CultureInfo.CurrentCulture,
                                            Strings.InvalidPartCreationPolicyOnImport,
                                            attributedImport.RequiredCreationPolicy),
                              origin);
                }
                return(new ReflectionParameterImportDefinition(
                           new Lazy <ParameterInfo>(() => parameter),
                           attributedImport.GetContractNameFromImport(importType),
                           attributedImport.GetTypeIdentityFromImport(importType),
                           CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                           attributedImport.Cardinality,
                           attributedImport.RequiredCreationPolicy,
                           CompositionServices.GetImportMetadata(importType, attributedImport),
                           origin));
            }
        }
        public static ReflectionMemberImportDefinition CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
        {
            Requires.NotNull(member, "member");

            ReflectionWritableMember reflectionMember = member.ToReflectionWritableMember();

            AttributedImportDefinitionCreationInfo importCreationInfo = AttributedModelDiscovery.GetImportDefinitionCreationInfo(reflectionMember, member);

            return(new ReflectionMemberImportDefinition(
                       new LazyMemberInfo(member),
                       importCreationInfo.ContractName,
                       importCreationInfo.RequiredTypeIdentity,
                       importCreationInfo.RequiredMetadata,
                       importCreationInfo.Cardinality,
                       importCreationInfo.IsRecomposable,
                       importCreationInfo.RequiredCreationPolicy,
                       origin));
        }
Beispiel #11
0
        public static ReflectionMemberImportDefinition CreateMemberImportDefinition(MemberInfo member, ICompositionElement origin)
        {
            Requires.NotNull(member, nameof(member));

            ReflectionWritableMember reflectionMember = member.ToReflectionWritableMember();
            IAttributedImport        attributedImport = AttributedModelDiscovery.GetAttributedImport(reflectionMember, member);
            ImportType importType = new ImportType(reflectionMember.ReturnType, attributedImport.Cardinality);

            if (importType.IsPartCreator)
            {
                return(new PartCreatorMemberImportDefinition(
                           new LazyMemberInfo(member),
                           origin,
                           new ContractBasedImportDefinition(
                               attributedImport.GetContractNameFromImport(importType),
                               attributedImport.GetTypeIdentityFromImport(importType),
                               CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                               attributedImport.Cardinality,
                               attributedImport.AllowRecomposition,
                               false,
                               CreationPolicy.NonShared,
                               CompositionServices.GetImportMetadata(importType, attributedImport))));
            }
            else
            {
                //Does this Import re-export the value if so, make it a rpe-requisite
                bool isPrerequisite = member.GetAttributes <ExportAttribute>().Length > 0;
                return(new ReflectionMemberImportDefinition(
                           new LazyMemberInfo(member),
                           attributedImport.GetContractNameFromImport(importType),
                           attributedImport.GetTypeIdentityFromImport(importType),
                           CompositionServices.GetRequiredMetadata(importType.MetadataViewType),
                           attributedImport.Cardinality,
                           attributedImport.AllowRecomposition,
                           isPrerequisite,
                           attributedImport.RequiredCreationPolicy,
                           CompositionServices.GetImportMetadata(importType, attributedImport),
                           origin));
            }
        }
        //
        // Import definition creation helpers
        //
        private static AttributedImportDefinitionCreationInfo GetImportDefinitionCreationInfo(ReflectionItem item, ICustomAttributeProvider attributeProvider)
        {
            Assumes.NotNull(item, attributeProvider);

            AttributedImportDefinitionCreationInfo importCreationInfo = new AttributedImportDefinitionCreationInfo();

            IAttributedImport attributedImport = AttributedModelDiscovery.GetAttributedImport(item, attributeProvider);
            ImportType        importType       = new ImportType(item.ReturnType, attributedImport.Cardinality);

            DisplayDebugWarnings(attributedImport.Cardinality, item, importType);

            importCreationInfo.RequiredMetadata = importType.IsLazy ?
                                                  CompositionServices.GetRequiredMetadata(importType.LazyType.MetadataViewType) :
                                                  Enumerable.Empty <string>();
            importCreationInfo.Cardinality            = attributedImport.Cardinality;
            importCreationInfo.ContractName           = attributedImport.GetContractNameFromImport(importType);
            importCreationInfo.RequiredTypeIdentity   = attributedImport.GetTypeIdentityFromImport(importType);
            importCreationInfo.IsRecomposable         = (item.ItemType == ReflectionItemType.Parameter) ? false : attributedImport.AllowRecomposition;
            importCreationInfo.RequiredCreationPolicy = attributedImport.RequiredCreationPolicy;

            return(importCreationInfo);
        }
        private IEnumerable <ImportDefinition> GetImportDefinitions()
        {
            List <ImportDefinition> imports = new List <ImportDefinition>();

            foreach (MemberInfo member in GetImportMembers(_type))
            {
                ReflectionMemberImportDefinition importDefinition = AttributedModelDiscovery.CreateMemberImportDefinition(member, this);
                imports.Add(importDefinition);
            }

            ConstructorInfo?constructor = GetConstructor();

            if (constructor != null)
            {
                foreach (ParameterInfo parameter in constructor.GetParameters())
                {
                    ReflectionParameterImportDefinition importDefinition = AttributedModelDiscovery.CreateParameterImportDefinition(parameter, this);
                    imports.Add(importDefinition);
                }
            }

            return(imports);
        }
        public void IsDiscoverable()
        {
            var expectations = new ExpectationCollection <Type, bool>();

            expectations.Add(typeof(ClassWithTwoZeroParameterConstructors), true);
            expectations.Add(typeof(SimpleConstructorInjectedObject), true);
            expectations.Add(typeof(StaticExportClass), true);
            expectations.Add(typeof(PublicComponentWithPublicExports), true);
            expectations.Add(typeof(ClassWithMultipleMarkedConstructors), true);
            expectations.Add(typeof(ClassWithNoMarkedOrDefaultConstructor), true);
            expectations.Add(typeof(ClassWhichOnlyHasDefaultConstructor), false);
            expectations.Add(typeof(ClassWithOnlyHasImportingConstructorButInherits), true);
            expectations.Add(typeof(ClassWithOnlyHasMultipleImportingConstructorButInherits), true);

            foreach (var e in expectations)
            {
                var definition = AttributedModelDiscovery.CreatePartDefinitionIfDiscoverable(e.Input, (ICompositionElement)null);

                bool result = (definition != null);

                Assert.Equal(e.Output, result);
            }
        }