Guest User

Untitled

a guest
Jun 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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.Linq;
  6. using System.Text;
  7.  
  8. namespace MefConsole
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var catalog = new AssemblyCatalog(typeof (Program).Assembly);
  15. var container = new CompositionContainer(catalog);
  16. var processor = container.GetExportedObject<OrderProcessor>();
  17.  
  18. processor.Process();
  19. Console.ReadLine();
  20. }
  21. }
  22.  
  23. public interface IRule
  24. {
  25. void Execute(object parameters);
  26. }
  27.  
  28. [Export]
  29. public class OrderProcessor
  30. {
  31. [ImportMany]
  32. public Export<IRule,IRuleMetadataView>[] rules { get; set; }
  33.  
  34. public void Process()
  35. {
  36. foreach (var ruleExport in rules)
  37. {
  38. Console.WriteLine(ruleExport.MetadataView.Description);
  39. var rule = ruleExport.GetExportedObject();
  40. rule.Execute(5);
  41. }
  42. }
  43. }
  44.  
  45. public interface IRuleMetadataView
  46. {
  47. string Description { get; }
  48. }
  49.  
  50. [MetadataAttribute]
  51. public class RuleMetadataAttribute : System.Attribute
  52. {
  53. public RuleMetadataAttribute(string description)
  54. {
  55. Description = description;
  56. }
  57. public string Description { get; set; }
  58. }
  59.  
  60. [RuleMetadata("Increments by one")]
  61. [Export(typeof(IRule))]
  62. public class IncrementByOneRule : IRule
  63. {
  64. public IncrementByOneRule()
  65. {
  66.  
  67. }
  68.  
  69. public void Execute(object parameters)
  70. {
  71. var value = (int)parameters;
  72. value = value + 1;
  73. Console.WriteLine(value);
  74. }
  75. }
  76.  
  77. [ExportMetadata("Description", "Subtracts by two")]
  78. [Export(typeof(IRule))]
  79. public class SubtractByTwoRule : IRule
  80. {
  81. public SubtractByTwoRule()
  82. {
  83.  
  84. }
  85. public void Execute(object parameters)
  86. {
  87. var value = (int)parameters;
  88. value = value - 2;
  89. Console.WriteLine(value);
  90. }
  91.  
  92. }
  93.  
  94.  
  95. }
Add Comment
Please, Sign In to add comment