private void ProcessPropertyAttributes() { foreach (PropertyInfo runtimeProperty in _exportType.GetRuntimeProperties()) { List<IExportCondition> exportConditions = new List<IExportCondition>(); List<IExportAttribute> exportAttributes = new List<IExportAttribute>(); IImportAttribute importAttribute = null; bool importAfterConstruction = false; object comparer = null; ExportStrategyFilter filter = null; foreach (Attribute customAttribute in runtimeProperty.GetCustomAttributes()) { if (customAttribute is IImportAttribute) { importAttribute = customAttribute as IImportAttribute; } IImportFilterAttribute filterAttribute = customAttribute as IImportFilterAttribute; if (filterAttribute != null) { filter = filterAttribute.ProvideFilter(_exportType, runtimeProperty.Name); } IImportSortCollectionAttribute sortAttribute = customAttribute as IImportSortCollectionAttribute; if (sortAttribute != null) { comparer = sortAttribute.ProvideComparer(_exportType, runtimeProperty.Name); } IExportAttribute exportAttribute = customAttribute as IExportAttribute; if (exportAttribute != null) { exportAttributes.Add(exportAttribute); } IExportConditionAttribute conditionAttribute = customAttribute as IExportConditionAttribute; if (conditionAttribute != null) { IExportCondition condition = conditionAttribute.ProvideCondition(runtimeProperty.PropertyType); if (condition != null) { exportConditions.Add(condition); } } IImportAfterConstructionAttribute afterConstruction = customAttribute as IImportAfterConstructionAttribute; if (afterConstruction != null) { importAfterConstruction = afterConstruction.ImportAfterConstruction(_exportType, runtimeProperty.PropertyType); } } foreach (IExportAttribute exportAttribute in exportAttributes) { IEnumerable<string> propertyExportNames = exportAttribute.ProvideExportNames(_exportType); IEnumerable<Type> propertyExportTypes = exportAttribute.ProvideExportTypes(_exportType); IExportCondition condition = null; if (exportConditions.Count > 0) { condition = new MultipleConditions(exportConditions.ToArray()); } ExportPropertyInfo exportPropertyInfo = new ExportPropertyInfo { ExportCondition = condition, ExportNames = propertyExportNames, ExportTypes = propertyExportTypes, PropertyInfo = runtimeProperty }; ExportProperty(exportPropertyInfo); } if (importAttribute != null && runtimeProperty.CanWrite) { ImportAttributeInfo attributeInfo = importAttribute.ProvideImportInfo(_exportType, runtimeProperty.Name); if (filter == null) { filter = attributeInfo.ExportStrategyFilter; } if (comparer == null) { comparer = attributeInfo.Comparer; } if (attributeInfo.ImportKey != null) { ExportStrategyFilter keyFilter = (context, strategy) => IExportLocatorExtensions.CompareKeyFunction(attributeInfo.ImportKey, context, strategy); filter = filter != null ? new ExportStrategyFilterGroup(keyFilter, filter) : keyFilter; } ImportPropertyInfo importPropertyInfo = new ImportPropertyInfo { ComparerObject = comparer, ExportStrategyFilter = filter, ImportName = attributeInfo.ImportName, IsRequired = attributeInfo.IsRequired, Property = runtimeProperty, ValueProvider = attributeInfo.ValueProvider, AfterConstruction = importAfterConstruction }; ImportProperty(importPropertyInfo); } } }
/// <summary> /// Creates a new closed export strategy that can be activated /// </summary> /// <param name="requestedType"></param> /// <returns></returns> public IExportStrategy CreateClosedStrategy(Type requestedType) { Type closedType = OpenGenericUtilities.CreateClosedExportTypeFromRequestingType(_exportType, requestedType); if (closedType != null) { TypeInfo closedTypeInfo = closedType.GetTypeInfo(); ClosedGenericExportStrategy newExportStrategy = new ClosedGenericExportStrategy(closedType); if (delegateInfo.ImportProperties != null) { foreach (ImportPropertyInfo importPropertyInfo in delegateInfo.ImportProperties) { PropertyInfo propertyInfo = closedTypeInfo.GetDeclaredProperty(importPropertyInfo.Property.Name); ImportPropertyInfo newPropertyInfo = new ImportPropertyInfo { Property = propertyInfo, ComparerObject = importPropertyInfo.ComparerObject, ExportStrategyFilter = importPropertyInfo.ExportStrategyFilter, ImportName = importPropertyInfo.ImportName, IsRequired = importPropertyInfo.IsRequired, ValueProvider = importPropertyInfo.ValueProvider }; newExportStrategy.ImportProperty(newPropertyInfo); } } if (delegateInfo.ImportMethods != null) { foreach (ImportMethodInfo importMethodInfo in delegateInfo.ImportMethods) { foreach (MethodInfo declaredMethod in closedTypeInfo.GetDeclaredMethods(importMethodInfo.MethodToImport.Name)) { ParameterInfo[] importMethodParams = importMethodInfo.MethodToImport.GetParameters(); ParameterInfo[] declaredMethodParams = declaredMethod.GetParameters(); if (importMethodParams.Length == declaredMethodParams.Length) { ImportMethodInfo newMethodInfo = new ImportMethodInfo { MethodToImport = declaredMethod }; // fill in method parameters // TODO: match the parameters to make sure they are the same newExportStrategy.ImportMethod(newMethodInfo); } } } } foreach (string exportName in base.ExportNames) { newExportStrategy.AddExportName(exportName); } if (_exportTypes != null) { foreach (Type type in _exportTypes) { Type newExportType = null; if (type.GetTypeInfo().IsInterface) { newExportType = closedType.GetTypeInfo() .ImplementedInterfaces.FirstOrDefault(x => x.GetTypeInfo().GUID == type.GetTypeInfo().GUID); } else { Type parentType = closedType.GetTypeInfo().BaseType; while (parentType != null && parentType.GetTypeInfo().GUID != type.GetTypeInfo().GUID) { parentType = parentType.GetTypeInfo().BaseType; } newExportType = parentType; } if (newExportType != null) { newExportStrategy.AddExportType(newExportType); } } } if (Lifestyle != null) { newExportStrategy.SetLifestyleContainer(Lifestyle.Clone()); } newExportStrategy.CreatingStrategy = this; if (_enrichWithDelegates != null) { foreach (var item in _enrichWithDelegates) { newExportStrategy.EnrichWithDelegate(item); } } foreach (var item in Metadata) { newExportStrategy.AddMetadata(item.Key, item.Value); } return newExportStrategy; } return null; }
public static IEnumerable<ImportPropertyInfo> ProcessImportPropertiesOnType(Type type) { foreach (PropertyInfo runtimeProperty in type.GetRuntimeProperties()) { List<IExportCondition> exportConditions = new List<IExportCondition>(); List<IExportAttribute> exportAttributes = new List<IExportAttribute>(); IImportAttribute importAttribute = null; object comparer = null; ExportStrategyFilter filter = null; foreach (Attribute customAttribute in runtimeProperty.GetCustomAttributes()) { if (customAttribute is IImportAttribute) { importAttribute = customAttribute as IImportAttribute; } IImportFilterAttribute filterAttribute = customAttribute as IImportFilterAttribute; if (filterAttribute != null) { filter = filterAttribute.ProvideFilter(type, runtimeProperty.Name); } IImportSortCollectionAttribute sortAttribute = customAttribute as IImportSortCollectionAttribute; if (sortAttribute != null) { comparer = sortAttribute.ProvideComparer(type, runtimeProperty.Name); } IExportAttribute exportAttribute = customAttribute as IExportAttribute; if (exportAttribute != null) { exportAttributes.Add(exportAttribute); } IExportConditionAttribute conditionAttribute = customAttribute as IExportConditionAttribute; if (conditionAttribute != null) { IExportCondition condition = conditionAttribute.ProvideCondition(runtimeProperty.PropertyType); if (condition != null) { exportConditions.Add(condition); } } } if (importAttribute != null && runtimeProperty.CanWrite) { ImportAttributeInfo attributeInfo = importAttribute.ProvideImportInfo(type, runtimeProperty.Name); if (filter == null) { filter = attributeInfo.ExportStrategyFilter; } if (comparer == null) { comparer = attributeInfo.Comparer; } if (attributeInfo.ImportKey != null) { ExportStrategyFilter keyFilter = (context, strategy) => IExportLocatorExtensions.CompareKeyFunction(attributeInfo.ImportKey, context, strategy); filter = filter != null ? new ExportStrategyFilterGroup(keyFilter, filter) : keyFilter; } ImportPropertyInfo importPropertyInfo = new ImportPropertyInfo { ComparerObject = comparer, ExportStrategyFilter = filter, ImportName = attributeInfo.ImportName, IsRequired = attributeInfo.IsRequired, Property = runtimeProperty, ValueProvider = attributeInfo.ValueProvider }; yield return importPropertyInfo; } } }
/// <summary> /// Configure the export to import a property /// </summary> /// <param name="propertyInfo"></param> public void ImportProperty(ImportPropertyInfo propertyInfo) { delegateInfo.ImportProperty(propertyInfo); }
/// <summary> /// Imports a property for the export /// </summary> /// <param name="info"></param> public void ImportProperty(ImportPropertyInfo info) { if (_importProperties == null) { _importProperties = new List<ImportPropertyInfo>(1); } _importProperties.Add(info); }