示例#1
0
 /// <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());
         }
     }
 }
示例#2
0
 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());
 }
示例#3
0
        /// <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);
            }
        }
示例#4
0
 /// <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);
     }
 }
示例#5
0
 public void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs)
     where T : IContainerBase
 => eventArgs.Target.RegisterType <IComponent1, Component1>();
示例#6
0
 public virtual void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs)
     where T : TTarget
 {
     checkDisposed();
     provideParts?.Invoke(new DelegateProvidePartsEventArgs <TTarget, T>(eventArgs));
 }
示例#7
0
 public void ProvideParts <T>(ProvidePartsEventArgs <T> eventArgs)
     where T : TTarget
 => (Participant as IProvideParts <TSuper>)?.ProvideParts(eventArgs);
示例#8
0
 /// <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));