Advertisement
Guest User

StackOverflow MefMultiExport

a guest
Jul 3rd, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.ComponentModel.Composition.Hosting;
  5. using System.Reflection;
  6.  
  7. namespace MefTry
  8. {
  9.     public class Program
  10.     {
  11.         [ImportMany]
  12.         public List<Lazy<IFoo, IFooMultiMeta>> Foos { get; set; }
  13.  
  14.         private static void Main ()
  15.         {
  16.             new Program().MainInternal();
  17.         }
  18.  
  19.         private void MainInternal ()
  20.         {
  21.             new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly())).ComposeParts(this);
  22.             foreach (Lazy<IFoo, IFooMultiMeta> lazyFoo in Foos)
  23.                 for (int i = 0; i < lazyFoo.Metadata.Name.Length; i++)
  24.                     Console.WriteLine("* {0} {1}", lazyFoo.Metadata.Name[i], lazyFoo.Metadata.Version[i]);
  25.             Console.WriteLine(Equals(Foos[0].Metadata, Foos[1].Metadata));
  26.             Console.ReadKey();
  27.         }
  28.     }
  29.  
  30.     public interface IFoo
  31.     {
  32.         void Do ();
  33.     }
  34.  
  35.     public interface IFooMeta
  36.     {
  37.         string Name { get; }
  38.         string Version { get; }
  39.     }
  40.  
  41.     public interface IFooMultiMeta
  42.     {
  43.         string[] Name { get; }
  44.         string[] Version { get; }
  45.     }
  46.  
  47.     [MetadataAttribute, AttributeUsage (AttributeTargets.Class, AllowMultiple = true)]
  48.     public class ExportFooAttribute : ExportAttribute, IFooMeta
  49.     {
  50.         public string Name { get; private set; }
  51.         public string Version { get; private set; }
  52.  
  53.         public ExportFooAttribute (string name, string version) : base(typeof(IFoo))
  54.         {
  55.             Name = name;
  56.             Version = version;
  57.         }
  58.     }
  59.  
  60.     [ExportFoo ("Bar", "1.0")]
  61.     [ExportFoo ("Baz", "1.0")]
  62.     [ExportFoo ("Baz", "2.0")]
  63.     public class Foo : IFoo
  64.     {
  65.         public void Do ()
  66.         {}
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement