Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. List<TheBaseClass> items = new List<TheBaseClass>()
  2.  
  3. public class ProcessingStep1()
  4. {
  5. Dictionary<Type, CalculateDelegate> lookupTable = new Dictionary<Type, CalculateDelegate>();
  6.  
  7. public ProcessingStep1()
  8. {
  9. lookupTable.Add(typeof(ItemA), ProcessItemA);
  10. lookupTable.Add(typeof(ItemB), ProcessItemB);
  11. // ...
  12. }
  13.  
  14. public method CalculateAThing(IEnumerable<TheBaseClass> items)
  15. {
  16. foreach (TheBaseClass item in items)
  17. {
  18. // desired to throw an exception if the key is not found
  19. CalculateDelegate calculate = lookupTable[item.GetType()];
  20. }
  21. }
  22.  
  23. public ProcessItemA(TheBaseClass item)
  24. {
  25. // need to cast to access a property from the derived type
  26. var something = (item as ItemA).PropertyOnItemA;
  27. item.BaseClassProperty = doMath(something);
  28. }
  29.  
  30. public ProcessItemB(TheBaseClass item)
  31. {
  32. // no need to cast, but the behavior is different for ItemB than others
  33. item.BaseClassProperty = somethingSpecific;
  34.  
  35. }
  36.  
  37. // ...
  38. }
  39.  
  40. public abstract class TheBaseClass
  41. {
  42. public abstract void CalculateAThing();
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement