Beispiel #1
0
        public override void Activate()
        {
            IDictionary <string, Lazy <object> > valuesLazy = ImportValues.ToDictionary(
                x => x.Key.MemberName,
                v => v.Value);

            if (CachedInstance == null && this.Definition.ParameterlessConstructor)
            {
                _cachedInstance = CustomActivator.CreateInstance(Definition.GetPartType());

                var unpackValues = valuesLazy.ToDictionary(k => k.Key, v => v.Value.Value);
                CustomActivator.PopulateObjectProperties(_cachedInstance, unpackValues);
            }
            else if (this.Definition.ParameterlessConstructor)
            {
                var unpackValues = valuesLazy.ToDictionary(k => k.Key, v => v.Value.Value);
                CustomActivator.PopulateObjectProperties(_cachedInstance, unpackValues);
            }
            else
            {
                var constructor        = Definition.GetConstructor();
                var requiredParameters = constructor.GetParameters();

                //Sorting imports to match constructor order
                object[] unpackValues = new SortedDictionary <string, Lazy <object> >(valuesLazy)
                                        .Zip(requiredParameters, (lazyValues, required) => new { lazyValues, required })
                                        .OrderBy(x => x.required.Name)
                                        .Select(x => x.lazyValues.Value.Value)
                                        .ToArray <object>();

                _cachedInstance = Definition.GetConstructor().Invoke(unpackValues);
            }
        }
        private bool TryGetImportValue(ImportDefinition definition, out object value)
        {
            lock (_lock)
            {
                if (_importValues != null && ImportValues.TryGetValue(definition, out value))
                {
                    ImportValues.Remove(definition);
                    return(true);
                }
            }

            value = null;
            return(false);
        }
        // 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 (_importValues == null || !ImportValues.ContainsKey(definition))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                      SR.InvalidOperation_GetExportedValueBeforePrereqImportSet,
                                                                      definition.ToElement().DisplayName));
                }
            }
        }