/// <summary> /// This method is virtual and implements /// <see cref="IProvideParts{TTarget}.ProvideParts{T}"/>. /// This implementation invokes all participants added here. /// Notice that each is invoked in a catch block, and this will trace exceptions, /// AND re-throw a new <see cref="AggregateException"/> if there are /// any exceptions. /// catch this exception and set the <see cref="IComposer{TTarget}.LastComposeErrors"/>. /// </summary> /// <param name="eventArgs"><see cref="IProvideParts{TTarget}"/> argument.</param> /// <exception cref="AggregateException"></exception> public virtual void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : TTarget { lock (Participants) { checkDisposed(); List <Exception> exceptions = new List <Exception>(Participants.Count); foreach (IProvideParts <TTarget> participant in Participants.OfType <IProvideParts <TTarget> >()) { try { participant.ProvideParts(eventArgs); } catch (Exception exception) { TraceSources.For(GetType()) .Error( exception, "Participant exception invoking ProvideParts: {0}, '{1}'.", participant, exception.Message); exceptions.Add(exception); } } if (exceptions.Count != 0) { throw new AggregateException( $"One or more exceptions was raised by a {GetType().GetFriendlyName()} participant in" + $" {nameof(IProvideParts<TTarget>.ProvideParts)}.", exceptions.EnumerateInReverse()); } } }
public void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : MyTarget { eventArgs.Target.AddPart(new MyPart()); eventArgs.Target.AddPart(new MyPart()); eventArgs.Target.AddPart(new MyPart()); }
/// <summary> /// This virtual method provides the implementation for /// <see cref="IProvideParts{TTarget}"/>. /// This enumerates the files on the <see cref="DirectoryWatcher"/> with the /// <see cref="SearchOption"/>, and loads each path with <see cref="TryLoadAssembly"/>. /// This method catches and traces all exceptions; and also provides support for /// <see cref="ProvideAssembliesOneTimeOnly"/>. /// </summary> public virtual void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : ContainerConfiguration { List <Assembly> assemblies = new List <Assembly>(); if (DirectoryWatcher.PathExists) { try { foreach (string filePath in Directory.EnumerateFiles( DirectoryWatcher.Path, DirectoryWatcher.Filter, SearchOption)) { if (TryLoadAssembly(filePath, out Assembly assembly)) { assemblies.Add(assembly); } } } catch (Exception exception) { TraceSources.For <MefDirectoryPartWatcher>() .Warning( exception, "Catching exception enumerating files in the watched directory: '{0}'.", exception.Message); } } if (assemblies.Count == 0) { return; } lock (ComposedAssemblies) { if (ProvideAssembliesOneTimeOnly) { foreach (Assembly assembly in new List <Assembly>(assemblies)) { if (!ComposedAssemblies.Contains(assembly)) { ComposedAssemblies.Add(assembly); } else { assemblies.Remove(assembly); } } if (assemblies.Count == 0) { return; } } } if (Conventions != null) { eventArgs.Target.WithAssemblies(assemblies, Conventions); } else { eventArgs.Target.WithAssemblies(assemblies); } }
/// <summary> /// This virtual method provides the implementation for /// <see cref="IProvideParts{TTarget}"/>. /// This adds all <see cref="ProvidedAssemblies"/>, with the optional /// <see cref="Conventions"/> if set. /// </summary> public virtual void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : ContainerConfiguration { if (Conventions != null) { eventArgs.Target.WithAssemblies(ProvidedAssemblies, Conventions); } else { eventArgs.Target.WithAssemblies(ProvidedAssemblies); } }
public void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : IContainerBase => eventArgs.Target.RegisterType <IComponent1, Component1>();
public virtual void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : TTarget { checkDisposed(); provideParts?.Invoke(new DelegateProvidePartsEventArgs <TTarget, T>(eventArgs)); }
public void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs) where T : TTarget => (Participant as IProvideParts <TSuper>)?.ProvideParts(eventArgs);
/// <summary> /// Constructor. /// </summary> /// <param name="eventArgs">Required.</param> /// <exception cref="ArgumentNullException"/> public DelegateProvidePartsEventArgs(ProvidePartsEventArgs <T> eventArgs) : base(eventArgs.Target, out _) => this.eventArgs = eventArgs ?? throw new ArgumentNullException(nameof(eventArgs));