Exemplo n.º 1
0
        /// <summary>
        /// Inspects the <paramref name="instance"/> for all properties that are marked with the <see cref="System.Composition.ImportAttribute"/> or <see cref="System.ComponentModel.Composition.ImportAttribute"/> attributes and resolves them.
        /// <para/>
        /// Use <see cref="IExportResolver.ComposeParts(object)"/> to resolve an objects dependencies when it cannot be exported into MEF (EG: Controls, IDE integration points).
        /// </summary>
        public virtual void ComposeParts(object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            using (Profiler.Profile())
            {
                var dependencies = DependencyHelper.FindDependencies(instance);

                foreach (var dep in dependencies)
                {
                    try
                    {
                        var value = GetExportedValue(dep.PropertyType);
                        dep.SetValue(instance, value);
                    }
                    catch (Exception ex)
                    {
                        log?.Exception(ex);
                        log?.Info("Failed to set " + dep.Name + " onto " + instance.GetType() + " (" + dep.PropertyType + ").");
                        Debugger.Break();
                    }
                }
            }
        }