Ejemplo n.º 1
0
        private static void ConfigureConstructorAttributes(ConstructorInfo constructorInfo, ref List <Tuple <object, List <Attribute> > > configuredMembers, Action <ParameterInfo, ImportConventionBuilder> configureConstuctorImports)
        {
            configuredMembers ??= new List <Tuple <object, List <Attribute> > >();

            // Make its attribute
            configuredMembers.Add(Tuple.Create((object)constructorInfo, s_importingConstructorList));

            //Okay we have the constructor now we can configure the ImportBuilders
            ParameterInfo[] parameterInfos = constructorInfo.GetParameters();
            foreach (ParameterInfo pi in parameterInfos)
            {
                bool isConfigured = pi.GetCustomAttributes <ImportAttribute>(false).FirstOrDefault() != null || pi.GetCustomAttributes <ImportManyAttribute>(false).FirstOrDefault() != null;
                if (isConfigured)
                {
                    CompositionTrace.Registration_ParameterImportConventionOverridden(pi, constructorInfo);
                }
                else
                {
                    var importBuilder = new ImportConventionBuilder();

                    // Let the developer alter them if they specified to do so
                    configureConstuctorImports?.Invoke(pi, importBuilder);

                    // Generate the attributes
                    List <Attribute> attributes = null;
                    importBuilder.BuildAttributes(pi.ParameterType, ref attributes);
                    configuredMembers.Add(Tuple.Create((object)pi, attributes));
                }
            }
        }
Ejemplo n.º 2
0
        public ImportParameter( ParameterInfo parameter, ImportConventionBuilder conventionBuilder )
        {
            Arg.NotNull( parameter, nameof( parameter ) );
            Arg.NotNull( conventionBuilder, nameof( conventionBuilder ) );

            this.parameter = parameter;
            this.conventionBuilder = conventionBuilder;
        }
Ejemplo n.º 3
0
            public void ConfigureConstructorImports(ParameterInfo parameterInfo, ImportConventionBuilder importBuilder)
            {
                if (_importBuilders != null)
                {
                    if (_importBuilders.TryGetValue(parameterInfo, out Action <ImportConventionBuilder> parameterImportBuilder))
                    {
                        parameterImportBuilder(importBuilder);
                    }
                }

                return;
            }
Ejemplo n.º 4
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;
        }
Ejemplo n.º 5
0
 public void ConfigureImport(PropertyInfo propertyInfo, ImportConventionBuilder importBuilder)
 {
     _configureImport?.Invoke(importBuilder);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MefImportConventionsBuilder"/> class.
 /// </summary>
 /// <param name="innerBuilder">
 /// The inner builder.
 /// </param>
 internal MefImportConventionsBuilder(ImportConventionBuilder innerBuilder)
 {
     this.innerBuilder = innerBuilder;
 }