Advertisement
Guest User

gblockmicrosoftcom

a guest
Mar 21st, 2009
1,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. /*
  2.  
  3. MEF does not allow recomposition on constructor imports, that is without a bit of help :-)
  4.  
  5. The helper class Recomposable<T> is a wrapper part that specifies a recomposable import that can be accessed through it's Value property. In order to use it, you need to manually register specific Recomposable<T>s for each type you would like to wrap.
  6.  
  7. In the sample below there are 2 loggers, DefaultLogger and OverridingLoggers which both export ILogger. UsesLogger imports Recomposable<ILogger> in the constructor. First we add the default logger to the container and compose. Then we see that the default logger's type name. Next we replace the DefaultLogger with the OverridingLogger and recompose and we see that it's name is outputted ot the console after composition.
  8.  
  9.  
  10. namespace ConstructorRecomposition
  11. {
  12.     public class Program
  13.     {
  14.         public static void Main(params string[] args)
  15.         {
  16.            
  17.             //manually create a catalog and the container
  18.             var catalog = new TypeCatalog(typeof (UsesLogger), typeof (Recomposable<ILogger>));
  19.             var container = new CompositionContainer(catalog);
  20.  
  21.             //create a batch, add the default logger and ocmpose
  22.             var batch = new CompositionBatch();
  23.             var defaultLogger = new DefaultLogger();
  24.             var defaultLoggerPart = batch.AddPart(defaultLogger);
  25.             container.Compose(batch);
  26.  
  27.             //grab the UsesLogger and output the logger that it immported
  28.             var usesLogger = container.GetExportedObject<UsesLogger>();
  29.             Console.WriteLine(usesLogger.Logger);
  30.  
  31.             //create a new batch and replace the default logger with an overriding logger and compose
  32.             batch = new CompositionBatch();
  33.             batch.RemovePart(defaultLoggerPart);
  34.             batch.AddPart(new OverridingLogger());
  35.             container.Compose(batch);
  36.            
  37.             //output the logger
  38.             Console.WriteLine(usesLogger.Logger);
  39.  
  40.             Console.ReadLine();
  41.         }
  42.     }
  43.  
  44.     [Export(typeof(ILogger))]
  45.     public class DefaultLogger : ILogger
  46.     {
  47.         public DefaultLogger()
  48.         {
  49.         }
  50.     }
  51.  
  52.     [Export(typeof(ILogger))]
  53.     public class OverridingLogger : ILogger
  54.     {
  55.         public OverridingLogger()
  56.         {
  57.         }
  58.     }
  59.  
  60.     public interface ILogger { }
  61.  
  62.     [Export]
  63.     public class UsesLogger
  64.     {
  65.         [ImportingConstructor]
  66.         public UsesLogger(Recomposable<ILogger> logger)
  67.         {
  68.             _recompLogger = logger;
  69.         }
  70.  
  71.         private Recomposable<ILogger> _recompLogger;
  72.  
  73.         public ILogger Logger { get { return _recompLogger.Value; } }
  74.     }
  75. }
  76. */
  77.  
  78.  
  79.     [Export]
  80.     public class Recomposable<T>
  81.     {
  82.         [Import(AllowRecomposition=true)]
  83.         public T Value { get; private set; }
  84.     }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement