private IEnumerable <DiscoveredExport> DiscoverInstanceExports(TypeInfo partType) { var partTypeAsType = partType.AsType(); foreach (var export in _attributeContext.GetDeclaredAttributes <ExportAttribute>(partTypeAsType, partType)) { IDictionary <string, object> metadata = new Dictionary <string, object>(); ReadMetadataAttribute(export, metadata); var applied = _attributeContext.GetDeclaredAttributes(partTypeAsType, partType); ReadLooseMetadata(applied, metadata); var contractType = export.ContractType ?? partTypeAsType; CheckInstanceExportCompatibility(partType, contractType.GetTypeInfo()); var exportKey = new CompositionContract(contractType, export.ContractName); if (metadata.Count == 0) { metadata = s_noMetadata; } yield return(new DiscoveredInstanceExport(exportKey, metadata)); } }
private IEnumerable <CompositionDependency> GetPartActivatorDependencies(DependencyAccessor definitionAccessor) { var partTypeAsType = _partType.AsType(); if (_constructor == null) { foreach (var c in _partType.DeclaredConstructors.Where(ci => ci.IsPublic && !(ci.IsStatic))) { if (_attributeContext.GetDeclaredAttribute <ImportingConstructorAttribute>(partTypeAsType, c) != null) { if (_constructor != null) { string message = SR.Format(SR.DiscoveredPart_MultipleImportingConstructorsFound, _partType); throw new CompositionFailedException(message); } _constructor = c; } } if (_constructor == null) { _constructor = _partType.DeclaredConstructors .FirstOrDefault(ci => ci.IsPublic && !(ci.IsStatic || ci.GetParameters().Any())); } if (_constructor == null) { string message = SR.Format(SR.DiscoveredPart_NoImportingConstructorsFound, _partType); throw new CompositionFailedException(message); } } var cps = _constructor.GetParameters(); for (var i = 0; i < cps.Length; ++i) { var pi = cps[i]; var site = new ParameterImportSite(pi); var importInfo = ContractHelpers.GetImportInfo(pi.ParameterType, _attributeContext.GetDeclaredAttributes(partTypeAsType, pi), site); if (!importInfo.AllowDefault) { yield return(definitionAccessor.ResolveRequiredDependency(site, importInfo.Contract, true)); } else { CompositionDependency optional; if (definitionAccessor.TryResolveOptionalDependency(site, importInfo.Contract, true, out optional)) { yield return(optional); } } } }
private static void SatisfyImportsInternal(this CompositionContext exportProvider, object objectWithLooseImports, AttributedModelProvider conventions) { if (exportProvider == null) { throw new ArgumentNullException(nameof(exportProvider)); } if (objectWithLooseImports == null) { throw new ArgumentNullException(nameof(objectWithLooseImports)); } if (conventions == null) { throw new ArgumentNullException(nameof(conventions)); } var objType = objectWithLooseImports.GetType(); foreach (var pi in objType.GetRuntimeProperties()) { ImportInfo importInfo; var site = new PropertyImportSite(pi); if (ContractHelpers.TryGetExplicitImportInfo(pi.PropertyType, conventions.GetDeclaredAttributes(pi.DeclaringType, pi), site, out importInfo)) { object value; if (exportProvider.TryGetExport(importInfo.Contract, out value)) { pi.SetValue(objectWithLooseImports, value); } else if (!importInfo.AllowDefault) { throw new CompositionFailedException(SR.Format( SR.CompositionContextExtensions_MissingDependency, pi.Name, objectWithLooseImports)); } } } var importsSatisfiedMethods = objectWithLooseImports.GetType().GetRuntimeMethods().Where(m => m.CustomAttributes.Any(ca => ca.AttributeType == typeof(OnImportsSatisfiedAttribute))); foreach (var ois in importsSatisfiedMethods) { ois.Invoke(objectWithLooseImports, null); } }
public override IEnumerable <CompositionDependency> GetDependencies(TypeInfo partType, DependencyAccessor definitionAccessor) { var partTypeAsType = partType.AsType(); var imports = (from pi in partTypeAsType.GetRuntimeProperties() .Where(pi => pi.CanWrite && pi.SetMethod.IsPublic && !(pi.SetMethod.IsStatic)) let attrs = _attributeContext.GetDeclaredAttributes(pi.DeclaringType, pi).ToArray() let site = new PropertyImportSite(pi) where attrs.Any(a => a is ImportAttribute || a is ImportManyAttribute) select new { Site = site, ImportInfo = ContractHelpers.GetImportInfo(pi.PropertyType, attrs, site) }).ToArray(); if (imports.Length == 0) { return(NoDependencies); } var result = new List <CompositionDependency>(); foreach (var i in imports) { if (!i.ImportInfo.AllowDefault) { result.Add(definitionAccessor.ResolveRequiredDependency(i.Site, i.ImportInfo.Contract, false)); } else { CompositionDependency optional; if (definitionAccessor.TryResolveOptionalDependency(i.Site, i.ImportInfo.Contract, false, out optional)) { result.Add(optional); } // Variation from CompositionContainer behaviour: we don't have to support recomposition // so we don't require that defaultable imports be set to null. } } return(result); }