Пример #1
0
        private static void ConfigureConstructorAttributes(ConstructorInfo constructorInfo, ref List <Tuple <object, List <Attribute> > > configuredMembers, Action <ParameterInfo, ImportBuilder> configureConstuctorImports)
        {
            if (configuredMembers == null)
            {
                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(typeof(ImportAttribute), false).FirstOrDefault() != null || pi.GetCustomAttributes(typeof(ImportManyAttribute), false).FirstOrDefault() != null;
                if (isConfigured)
                {
                    CompositionTrace.Registration_ParameterImportConventionOverridden(pi, constructorInfo);
                }
                else
                {
                    var importBuilder = new ImportBuilder();

                    // 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));
                }
            }
        }
Пример #2
0
 public void ConfigureImport(PropertyInfo propertyInfo, ImportBuilder importBuilder)
 {
     if(this._configureImport != null)
     {
         this._configureImport(importBuilder);
     }
 }
Пример #3
0
 public void ConfigureConstructorImports(ParameterInfo parameterInfo, ImportBuilder importBuilder)
 {
     if (_importBuilders != null &&
         _importBuilders.TryGetValue(parameterInfo, out Action <ImportBuilder> parameterImportBuilder))
     {
         parameterImportBuilder(importBuilder);
     }
 }
Пример #4
0
            public void ConfigureConstructorImports(ParameterInfo parameterInfo, ImportBuilder importBuilder)
            {
                if(this._importBuilders != null)
                {
                    Action<ImportBuilder> parameterImportBuilder;
                    if(this._importBuilders.TryGetValue(parameterInfo, out parameterImportBuilder))
                    {
                        parameterImportBuilder(importBuilder);
                    }
                }

                return;
            }
Пример #5
0
 public void ConfigureImport(PropertyInfo propertyInfo, ImportBuilder importBuilder)
 {
     _configureImport?.Invoke(importBuilder);
 }
Пример #6
0
        internal void BuildPropertyAttributes(Type type, ref List <Tuple <object, List <Attribute> > > configuredMembers)
        {
            if (_propertyImports.Any() || _propertyExports.Any())
            {
                foreach (PropertyInfo pi in type.GetProperties())
                {
                    List <Attribute> attributes = null;
                    PropertyInfo     declaredPi = pi.DeclaringType.UnderlyingSystemType.GetProperty(pi.Name, pi.PropertyType);
                    int  importsBuilt           = 0;
                    bool checkedIfConfigured    = false;
                    bool isConfigured           = false;

                    // Run through the import specifications see if any match
                    foreach (Tuple <Predicate <PropertyInfo>, Action <PropertyInfo, ImportBuilder>, Type> importSpecification in _propertyImports)
                    {
                        if (importSpecification.Item1 != null && importSpecification.Item1(declaredPi))
                        {
                            var importBuilder = new ImportBuilder();

                            if (importSpecification.Item3 != null)
                            {
                                importBuilder.AsContractType(importSpecification.Item3);
                            }

                            importSpecification.Item2?.Invoke(declaredPi, importBuilder);

                            if (!checkedIfConfigured)
                            {
                                isConfigured        = pi.GetCustomAttributes(typeof(ImportAttribute), false).FirstOrDefault() != null || pi.GetCustomAttributes(typeof(ImportManyAttribute), false).FirstOrDefault() != null;
                                checkedIfConfigured = true;
                            }

                            if (isConfigured)
                            {
                                CompositionTrace.Registration_MemberImportConventionOverridden(type, pi);
                                break;
                            }
                            else
                            {
                                importBuilder.BuildAttributes(declaredPi.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 (Tuple <Predicate <PropertyInfo>, Action <PropertyInfo, ExportBuilder>, Type> exportSpecification in _propertyExports)
                    {
                        if (exportSpecification.Item1 != null && exportSpecification.Item1(declaredPi))
                        {
                            var exportBuilder = new ExportBuilder();

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

                            exportSpecification.Item2?.Invoke(declaredPi, exportBuilder);

                            if (!checkedIfConfigured)
                            {
                                isConfigured        = pi.GetCustomAttributes(typeof(ExportAttribute), false).FirstOrDefault() != null || MemberHasExportMetadata(pi);
                                checkedIfConfigured = true;
                            }

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

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

                        configuredMembers.Add(Tuple.Create((object)declaredPi, attributes));
                    }
                }
            }
        }