Example #1
0
        internal void BuildPropertyAttributes(Type type, ref List <Tuple <object, List <Attribute> > > configuredMembers)
        {
            if (this._propertyImports.Any() || this._propertyExports.Any())
            {
                foreach (var pi in type.GetRuntimeProperties())
                {
                    List <Attribute> attributes = null;
                    int  importsBuilt           = 0;
                    bool checkedIfConfigured    = false;
                    bool isConfigured           = false;

                    PropertyInfo underlyingPi = null;

                    // Run through the import specifications see if any match
                    foreach (var importSpecification in this._propertyImports)
                    {
                        if (underlyingPi == null)
                        {
                            underlyingPi = pi.DeclaringType.GetRuntimeProperty(pi.Name);
                        }
                        if (importSpecification.Item1 != null && importSpecification.Item1(underlyingPi))
                        {
                            var importBuilder = new ImportConventionBuilder();

                            if (importSpecification.Item2 != null)
                            {
                                importSpecification.Item2(pi, importBuilder);
                            }

                            if (!checkedIfConfigured)
                            {
                                isConfigured        = pi.GetFirstAttribute <ImportAttribute>() != null || pi.GetFirstAttribute <ImportManyAttribute>() != null;
                                checkedIfConfigured = true;
                            }

                            if (isConfigured)
                            {
                                CompositionTrace.Registration_MemberImportConventionOverridden(type, pi);
                                break;
                            }
                            else
                            {
                                importBuilder.BuildAttributes(pi.PropertyType, ref attributes);
                                ++importsBuilt;
                            }
                        }
                        if (importsBuilt > 1)
                        {
                            CompositionTrace.Registration_MemberImportConventionMatchedTwice(type, pi);
                        }
                    }

                    checkedIfConfigured = false;
                    isConfigured        = false;

                    // Run through the export specifications see if any match
                    foreach (var exportSpecification in this._propertyExports)
                    {
                        if (underlyingPi == null)
                        {
                            underlyingPi = pi.DeclaringType.GetRuntimeProperty(pi.Name);
                        }

                        if (exportSpecification.Item1 != null && exportSpecification.Item1(underlyingPi))
                        {
                            var exportBuilder = new ExportConventionBuilder();

                            if (exportSpecification.Item3 != null)
                            {
                                exportBuilder.AsContractType(exportSpecification.Item3);
                            }

                            if (exportSpecification.Item2 != null)
                            {
                                exportSpecification.Item2(pi, exportBuilder);
                            }

                            if (!checkedIfConfigured)
                            {
                                isConfigured        = pi.GetFirstAttribute <ExportAttribute>() != null || MemberHasExportMetadata(pi);
                                checkedIfConfigured = true;
                            }

                            if (isConfigured)
                            {
                                CompositionTrace.Registration_MemberExportConventionOverridden(type, pi);
                                break;
                            }
                            else
                            {
                                exportBuilder.BuildAttributes(pi.PropertyType, ref attributes);
                            }
                        }
                    }

                    if (attributes != null)
                    {
                        if (configuredMembers == null)
                        {
                            configuredMembers = new List <Tuple <object, List <Attribute> > >();
                        }

                        configuredMembers.Add(Tuple.Create((object)pi, attributes));
                    }
                }
            }
            return;
        }
Example #2
0
        internal IEnumerable <Attribute> BuildTypeAttributes(Type type)
        {
            var attributes = new List <Attribute>();

            if (this._typeExportBuilders != null)
            {
                bool isConfigured = type.GetTypeInfo().GetFirstAttribute <ExportAttribute>() != null || MemberHasExportMetadata(type.GetTypeInfo());
                if (isConfigured)
                {
                    CompositionTrace.Registration_TypeExportConventionOverridden(type);
                }
                else
                {
                    foreach (var export in this._typeExportBuilders)
                    {
                        export.BuildAttributes(type, ref attributes);
                    }
                }
            }

            if (this._isShared)
            {
                // Check if there is already a SharedAttribute.  If found Trace a warning and do not add this Shared
                // otherwise add new one
                bool isConfigured = type.GetTypeInfo().GetFirstAttribute <SharedAttribute>() != null;
                if (isConfigured)
                {
                    CompositionTrace.Registration_PartCreationConventionOverridden(type);
                }
                else
                {
                    attributes.Add(this._sharingBoundary == null ?
                                   new SharedAttribute() :
                                   new SharedAttribute(this._sharingBoundary));
                }
            }

            //Add metadata attributes from direct specification
            if (this._metadataItems != null)
            {
                bool isConfigured = type.GetTypeInfo().GetFirstAttribute <PartMetadataAttribute>() != null;
                if (isConfigured)
                {
                    CompositionTrace.Registration_PartMetadataConventionOverridden(type);
                }
                else
                {
                    foreach (var item in this._metadataItems)
                    {
                        attributes.Add(new PartMetadataAttribute(item.Item1, item.Item2));
                    }
                }
            }

            //Add metadata attributes from func specification
            if (this._metadataItemFuncs != null)
            {
                bool isConfigured = type.GetTypeInfo().GetFirstAttribute <PartMetadataAttribute>() != null;
                if (isConfigured)
                {
                    CompositionTrace.Registration_PartMetadataConventionOverridden(type);
                }
                else
                {
                    foreach (var item in this._metadataItemFuncs)
                    {
                        var name  = item.Item1;
                        var value = (item.Item2 != null) ? item.Item2(type) : null;
                        attributes.Add(new PartMetadataAttribute(name, value));
                    }
                }
            }

            if (this._interfaceExports.Any())
            {
                if (this._typeExportBuilders != null)
                {
                    bool isConfigured = type.GetTypeInfo().GetFirstAttribute <ExportAttribute>() != null || MemberHasExportMetadata(type.GetTypeInfo());
                    if (isConfigured)
                    {
                        CompositionTrace.Registration_TypeExportConventionOverridden(type);
                    }
                    else
                    {
                        foreach (var iface in type.GetTypeInfo().ImplementedInterfaces)
                        {
                            if (iface == typeof(IDisposable))
                            {
                                continue;
                            }

                            // Run through the export specifications see if any match
                            foreach (var exportSpecification in this._interfaceExports)
                            {
                                if (exportSpecification.Item1 != null && exportSpecification.Item1(iface))
                                {
                                    ExportConventionBuilder exportBuilder = new ExportConventionBuilder();
                                    exportBuilder.AsContractType(iface);
                                    if (exportSpecification.Item2 != null)
                                    {
                                        exportSpecification.Item2(iface, exportBuilder);
                                    }
                                    exportBuilder.BuildAttributes(iface, ref attributes);
                                }
                            }
                        }
                    }
                }
            }
            return(attributes);
        }