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.Linq;
- using System.Text;
- namespace MefConsole
- {
- class Program
- {
- static void Main(string[] args)
- {
- var catalog = new AssemblyCatalog(typeof (Program).Assembly);
- var container = new CompositionContainer(catalog);
- var processor = container.GetExportedObject<OrderProcessor>();
- processor.Process();
- Console.ReadLine();
- }
- }
- public interface IRule
- {
- void Execute(object parameters);
- }
- [Export]
- public class OrderProcessor
- {
- [ImportMany]
- public Export<IRule,IRuleMetadataView>[] rules { get; set; }
- public void Process()
- {
- foreach (var ruleExport in rules)
- {
- Console.WriteLine(ruleExport.MetadataView.Description);
- var rule = ruleExport.GetExportedObject();
- rule.Execute(5);
- }
- }
- }
- public interface IRuleMetadataView
- {
- string Description { get; }
- }
- [MetadataAttribute]
- public class RuleMetadataAttribute : System.Attribute
- {
- public RuleMetadataAttribute(string description)
- {
- Description = description;
- }
- public string Description { get; set; }
- }
- [RuleMetadata("Increments by one")]
- [Export(typeof(IRule))]
- public class IncrementByOneRule : IRule
- {
- public IncrementByOneRule()
- {
- }
- public void Execute(object parameters)
- {
- var value = (int)parameters;
- value = value + 1;
- Console.WriteLine(value);
- }
- }
- [ExportMetadata("Description", "Subtracts by two")]
- [Export(typeof(IRule))]
- public class SubtractByTwoRule : IRule
- {
- public SubtractByTwoRule()
- {
- }
- public void Execute(object parameters)
- {
- var value = (int)parameters;
- value = value - 2;
- Console.WriteLine(value);
- }
- }
- }
Add Comment
Please, Sign In to add comment