Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition;
- using System.ComponentModel.Composition.Hosting;
- using System.Reflection;
- namespace MefTry
- {
- public class Program
- {
- [ImportMany]
- public List<Lazy<IFoo, IFooMultiMeta>> Foos { get; set; }
- private static void Main ()
- {
- new Program().MainInternal();
- }
- private void MainInternal ()
- {
- new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly())).ComposeParts(this);
- foreach (Lazy<IFoo, IFooMultiMeta> lazyFoo in Foos)
- for (int i = 0; i < lazyFoo.Metadata.Name.Length; i++)
- Console.WriteLine("* {0} {1}", lazyFoo.Metadata.Name[i], lazyFoo.Metadata.Version[i]);
- Console.WriteLine(Equals(Foos[0].Metadata, Foos[1].Metadata));
- Console.ReadKey();
- }
- }
- public interface IFoo
- {
- void Do ();
- }
- public interface IFooMeta
- {
- string Name { get; }
- string Version { get; }
- }
- public interface IFooMultiMeta
- {
- string[] Name { get; }
- string[] Version { get; }
- }
- [MetadataAttribute, AttributeUsage (AttributeTargets.Class, AllowMultiple = true)]
- public class ExportFooAttribute : ExportAttribute, IFooMeta
- {
- public string Name { get; private set; }
- public string Version { get; private set; }
- public ExportFooAttribute (string name, string version) : base(typeof(IFoo))
- {
- Name = name;
- Version = version;
- }
- }
- [ExportFoo ("Bar", "1.0")]
- [ExportFoo ("Baz", "1.0")]
- [ExportFoo ("Baz", "2.0")]
- public class Foo : IFoo
- {
- public void Do ()
- {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement