private void ProcessMethodAttributes() { foreach (MethodInfo declaredMethod in _exportType.GetRuntimeMethods()) { if (declaredMethod.IsPublic && !declaredMethod.IsStatic) { foreach (Attribute customAttribute in declaredMethod.GetCustomAttributes()) { IImportAttribute attribute = customAttribute as IImportAttribute; if (attribute != null) { ImportMethodInfo methodInfo = new ImportMethodInfo { MethodToImport = declaredMethod }; ImportMethod(methodInfo); break; } if (customAttribute is IActivationCompleteAttribute) { ActivateMethod(declaredMethod); break; } if (customAttribute is ICustomInitializationAttribute) { break; } ICustomEnrichmentExpressionAttribute enrichmentAttribute = customAttribute as ICustomEnrichmentExpressionAttribute; if (enrichmentAttribute != null) { ICustomEnrichmentLinqExpressionProvider provider = enrichmentAttribute.GetProvider(ActivationType, declaredMethod); if (provider != null) { EnrichWithExpression(provider); } } } } } }
/// <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<ImportMethodInfo> ProcessMethodAttributesOnType(Type type) { foreach (MethodInfo declaredMethod in type.GetRuntimeMethods()) { if (declaredMethod.IsPublic && !declaredMethod.IsStatic) { foreach (Attribute customAttribute in declaredMethod.GetCustomAttributes()) { IImportAttribute attribute = customAttribute as IImportAttribute; if (attribute != null) { ImportMethodInfo methodInfo = new ImportMethodInfo { MethodToImport = declaredMethod }; yield return methodInfo; break; } } } } }
/// <summary> /// Configure the export to import a method /// </summary> /// <param name="methodInfo"></param> public void ImportMethod(ImportMethodInfo methodInfo) { delegateInfo.ImportMethod(methodInfo); }
/// <summary> /// Mark a method for importing /// </summary> public void ImportMethod(ImportMethodInfo info) { if (_importMethods == null) { _importMethods = new List<ImportMethodInfo>(1); } _importMethods.Add(info); }