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
19 private static readonly log4net.ILog Log = log4net.LogManager.GetLogger("Pithos.Extensions.IoC");
24 public CompositionContainer Container;
27 /// Layily initialized singleton instance of IoC
29 static readonly Lazy<IoC> Instance=new Lazy<IoC>();
32 /// Gets the singleton instance of IoC
34 public static IoC Current
36 get { return Instance.Value; }
40 /// Initializes a new instance of the IoC from types exported from the executing assembly
44 var catalog = new AggregateCatalog();
45 catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
47 Container=new CompositionContainer(catalog);
51 /// Initializes all imported properties of the target object
53 /// <typeparam name="T">Type of the target object</typeparam>
54 /// <param name="target">The target object</param>
55 /// <returns>The object with all imported properties initialized</returns>
56 public T Compose<T>(T target)
60 Container.ComposeParts(target);
65 Log.ErrorFormat("Composition Error: {0}",exc);