Language: C#
Repeatable MEF metadata
1: class Program 2: { 3: static void Main(string[] args) 4: { 5: var instance = new Program(); 6: var cat = new AssemblyCatalog(typeof(Program).Assembly); 7: var container = new CompositionContainer(cat); 8: container.ComposeParts(instance); 9: 10: foreach (var plug in instance.Plugins) 11: { 12: foreach (var category in plug.Metadata.Categories) 13: { 14: Console.ForegroundColor = ConsoleColor.Yellow; 15: Console.Write("{0}, ", category); 16: Console.ResetColor(); 17: } 18: Console.WriteLine(string.Empty); 19: plug.Value.Write(); 20: } 21: Console.ReadLine(); 22: } 23: 24: [ImportMany] 25: public Lazy<IPlug, IMetadataView>[] Plugins { get; private set; } 26: } 27: 28: public enum Category 29: { 30: Travel, Game, City, News 31: } 32: 33: //[InheritedExport] // do not use inherited export (for some reason it don't working well with metadata) 34: public interface IPlug 35: { 36: void Write(); 37: } 38: 39: public interface IMetadataView 40: { 41: Category[] Categories { get; } 42: } 43: 44: [MetadataAttribute] 45: [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] 46: public class CategoryAttribute : Attribute 47: { 48: public CategoryAttribute(Category category) 49: { 50: Categories = category; 51: } 52: public Category Categories { get; private set; } 53: } 54: 55: [Export(typeof(IPlug))] 56: [ExportMetadata("Categories", Category.Travel, IsMultiple = true)] // using untyped metadata considered as bad practice 57: [ExportMetadata("Categories", Category.City, IsMultiple = true)] // using untyped metadata considered as bad practice 58: public class NewYork : IPlug 59: { 60: public void Write() { Console.WriteLine("New York"); } 61: } 62: 63: [Export(typeof(IPlug))] 64: [Category(Category.Game)] 65: [Category(Category.News)] 66: public class Football : IPlug 67: { 68: public void Write() { Console.WriteLine("Football"); } 69: }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

