/// <summary>
 /// Recupera o valor exportado.
 /// </summary>
 /// <param name="definition"></param>
 /// <returns></returns>
 /// <exception cref="InvalidOperationException"></exception>
 public override object GetExportedValue(System.ComponentModel.Composition.Primitives.ExportDefinition definition)
 {
     try
     {
         ProviderExportDefinition export = definition as ProviderExportDefinition;
         if (export == null)
         {
             throw new InvalidOperationException("The supplied export definition was of an unknown type.");
         }
         if (export.Member == null)
         {
             throw new InvalidOperationException(string.Format("Not found member to export definition. {0}", export.ContractName));
         }
         ComposableMember exportable = export.Member.ToComposableMember();
         object           instance   = null;
         if (exportable.IsInstanceNeeded)
         {
             instance = this.GetActivatedInstance(exportable);
         }
         object value = exportable.GetValue(instance);
         return(value);
     }
     catch (Exception ex)
     {
         ComposablePartErrorHandler.NotifyGetExportedValueError(definition, ex);
         throw;
     }
 }
        /// <summary>
        ///
        /// </summary>
        private void SatisfyPostCompositionImports()
        {
            IEnumerable <System.ComponentModel.Composition.Primitives.ImportDefinition> members = this.ImportDefinitions.Where(import => !import.IsPrerequisite);

            foreach (var i in members)
            {
                var definition = i as ProviderImportDefinition;
                if (definition == null)
                {
                    continue;
                }
                ImportableInfo value;
                if (this.ImportedValues.TryGetValue(definition, out value))
                {
                    ComposableMember importable = definition.Member.ToComposableMember();
                    importable.SetValue(this.Instance, value.GetValue());
                }
            }
        }
            /// <summary>
            /// Recupera o valor.
            /// </summary>
            /// <returns></returns>
            public object GetValue()
            {
                ComposableMember importable = null;

                if (_import == null)
                {
                    if (_definition is ProviderParameterImportDefinition)
                    {
                        importable = ((ProviderParameterImportDefinition)_definition).Parameter.ToComposableMember();
                    }
                    else
                    {
                        throw new InvalidOperationException("The supplied import definition was of an unknown type.");
                    }
                }
                else
                {
                    importable = _import.Member.ToComposableMember();
                }
                object value = importable.GetImportValueFromExports(_exports);

                return(value);
            }
        /// <summary>
        /// Creates a new instance of the <see cref="ComposableMember"/>, specified by the parameter, if it's needed.
        /// </summary>
        /// <param name="exportable">The exportable member to create an instance of, if it is required.</param>
        /// <returns>An instance of the object specified by the provided <see cref="ComposableMember"/>.</returns>
        private object GetActivatedInstance(ComposableMember exportable)
        {
            if (Definition.PartCreationPolicy == System.ComponentModel.Composition.CreationPolicy.Shared && this.Instance != null)
            {
                return(this.Instance);
            }
            var constructor = exportable as ComposableConstructor;

            if (constructor == null)
            {
                try
                {
                    if (this.Definition.UseDispatcher && Colosoft.Threading.DispatcherManager.Dispatcher != null && Colosoft.Threading.DispatcherManager.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
                    {
                        this.Instance = Colosoft.Threading.DispatcherManager.Dispatcher.Invoke(new Func <Type, object>(Activator.CreateInstance), new object[] {
                            exportable.DeclaringType
                        });
                    }
                    else
                    {
                        this.Instance = Activator.CreateInstance(exportable.DeclaringType);
                    }
                }
                catch (Exception ex)
                {
                    if (ex is System.Reflection.TargetInvocationException && ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }
                    throw new ActivatedInstanceException(string.Format("An error ocurred when create instance for type '{0}'. \r\nMessage: {1}", exportable.DeclaringType, ex.Message), ex);
                }
            }
            else
            {
                var ctorParameters = constructor.Constructor.GetParameters();
                var parameters     = new object[ctorParameters.Length];
                var i = 0;
                foreach (var j in Definition.ImportsParametersDefinitions)
                {
                    var value = this.ImportedValues[j];
                    value.ClearExportValue();
                    try
                    {
                        parameters[i] = value.GetValue();
                    }
                    catch (Exception ex)
                    {
                        throw new ComposableMemberParameterException(exportable, j, ex);
                    }
                    i++;
                }
                try
                {
                    if (this.Definition.UseDispatcher && Colosoft.Threading.DispatcherManager.Dispatcher != null && Colosoft.Threading.DispatcherManager.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
                    {
                        var declaringType = exportable.DeclaringType;
                        this.Instance = Colosoft.Threading.DispatcherManager.Dispatcher.Invoke(new Func <Type, object[], object>(Activator.CreateInstance), new object[] {
                            declaringType,
                            parameters
                        });
                    }
                    else
                    {
                        this.Instance = constructor.Constructor.Invoke(parameters);
                    }
                }
                catch (Exception ex)
                {
                    if (ex is System.Reflection.TargetInvocationException && ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }
                    throw new ActivatedInstanceException(string.Format("An error ocurred when create instance for type '{0}'. \r\nMessage: {1}", exportable.DeclaringType, ex.Message), ex);
                }
            }
            if (this.IsComposed)
            {
                this.SatisfyPostCompositionImports();
            }
            return(this.Instance);
        }
Esempio n. 5
0
 /// <summary>
 /// Construtro padrão.
 /// </summary>
 /// <param name="member"></param>
 /// <param name="parameter"></param>
 /// <param name="innerException"></param>
 internal ComposableMemberParameterException(ComposableMember member, ProviderParameterImportDefinition parameter, Exception innerException) : base(ResourceMessageFormatter.Create(() => Properties.Resources.ComposableMemberParameterExcepton_Message, parameter != null ? parameter.Parameter.Name : null, parameter != null ? parameter.Parameter.ParameterType.FullName : null, member != null ? member.DeclaringType.FullName : null).Format(), innerException)
 {
     Member    = member;
     Parameter = parameter;
 }
 /// <summary>
 /// Cria um <see cref="ComposableMember"/> para uma instancia de <see cref="System.Reflection.MemberInfo"/>.
 /// </summary>
 /// <param name="member">Instancia do <see cref="System.Reflection.MemberInfo"/> para criar o <see cref="ComposableMember"/>.</param>
 /// <returns>A <see cref="ComposableMember"/> instance.</returns>
 public static ComposableMember ToComposableMember(this System.Reflection.MemberInfo member)
 {
     return(ComposableMember.Create(member));
 }