1 // <copyright file="IoC.cs" company="GRNet">
2 // This project is open source. Released under the XYZ license
5 namespace Pithos.ShellExtensions
8 using System.ComponentModel.Composition;
9 using System.ComponentModel.Composition.Hosting;
10 using System.Diagnostics;
11 using System.Reflection;
14 /// The IoC class initializes MEF DI and acts as a service locator.
15 /// A singleton instance of IoC is provided by Instance
22 public CompositionContainer Container;
25 /// Layily initialized singleton instance of IoC
27 static readonly Lazy<IoC> Instance=new Lazy<IoC>();
30 /// Gets the singleton instance of IoC
32 public static IoC Current
34 get { return Instance.Value; }
38 /// Initializes a new instance of the IoC from types exported from the executing assembly
42 var catalog = new AggregateCatalog();
43 catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
45 Container=new CompositionContainer(catalog);
49 /// Initializes all imported properties of the target object
51 /// <typeparam name="T">Type of the target object</typeparam>
52 /// <param name="target">The target object</param>
53 /// <returns>The object with all imported properties initialized</returns>
54 public T Compose<T>(T target)
58 Container.ComposeParts(target);
63 Trace.TraceError("Composition Error: {0}",exc);