private void SetPrerequisiteImports() { IEnumerable <ImportDefinition> members = ImportDefinitions.Where(import => import.IsPrerequisite); // NOTE: Dev10 484204 The validation is turned off for post imports because of it broke declarative composition UseImportedValues(members, SetExportedValueForImport, false); }
private object[] GetConstructorArguments() { ReflectionParameterImportDefinition[] parameterImports = ImportDefinitions.OfType <ReflectionParameterImportDefinition>().ToArray(); object[] arguments = new object[parameterImports.Length]; UseImportedValues( parameterImports, (import, definition, value) => { if (definition.Cardinality == ImportCardinality.ZeroOrMore && !import.ImportType.IsAssignableCollectionType) { throw new ComposablePartException( string.Format(CultureInfo.CurrentCulture, SR.ReflectionModel_ImportManyOnParameterCanOnlyBeAssigned, Definition.GetPartType().FullName, definition.ImportingLazyParameter.Value.Name), Definition.ToElement()); } arguments[definition.ImportingLazyParameter.Value.Position] = value; }, true); return(arguments); }
/// <summary> /// Create a PartDefinitionInfo representing partDefinition. /// </summary> /// <param name="partDefinition">The part to be analyzed.</param> public PartDefinitionInfo(ComposablePartDefinition partDefinition) { PartDefinition = partDefinition; ImportDefinitions = Enumerable.Empty <ImportDefinitionInfo>(); _isRejected = new Lazy <bool>(() => ImportDefinitions .Any(idi => idi.Exception is ImportCardinalityMismatchException)); }
/// <summary> /// If the part is rejected, find the most likely parts in the composition /// that caused this rejection. /// </summary> /// <param name="maximumAnalysisDepth">The maximum depth of the composition /// graph to be inspected.</param> /// <returns></returns> public IEnumerable <PartDefinitionInfo> FindPossibleRootCauses(int maximumAnalysisDepth) { return(ImportDefinitions .Where(id => id.IsBroken) .SelectMany(id => id .UnsuitableExportDefinitions .Where(ed => ed.PartDefinition.IsRejected && ed.PartDefinition != this) .SelectMany(ed => ed.PartDefinition.FindPossibleRootCauses(--maximumAnalysisDepth)) .Take(maximumAnalysisDepth)) .Append(this) .Where(pd => pd.IsPrimaryRejection) .Distinct() .ToArray()); }
// alwayc called under a lock private bool RequiresActivation() { // If we have any imports then we need activation // (static imports are not supported) if (ImportDefinitions.Any()) { return(true); } // If we have any instance exports, then we also // need activation. return(ExportDefinitions.Any(definition => { ExportingMember member = GetExportingMemberFromDefinition(definition); return member.RequiresInstance; })); }
// this is called under a lock private void EnsureGettable() { // If we're already composed then we know that // all pre-req imports have been satisfied if (_initialCompositionComplete) { return; } // Make sure all pre-req imports have been set foreach (ImportDefinition definition in ImportDefinitions.Where(definition => definition.IsPrerequisite)) { if (!this._importValues.ContainsKey(definition)) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Strings.InvalidOperation_GetExportedValueBeforePrereqImportSet, definition.ToElement().DisplayName)); } } }
/// <summary> /// Determine whether the part imports a contract. /// </summary> /// <param name="contract">Name of the contract to check for.</param> /// <returns>True if the part imports the contract.</returns> public bool ImportsContract(string contract) { return(ImportDefinitions.Any(id => id.ImportDefinition.ContractName == contract)); }