Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<TheBaseClass> items = new List<TheBaseClass>()
- public class ProcessingStep1()
- {
- Dictionary<Type, CalculateDelegate> lookupTable = new Dictionary<Type, CalculateDelegate>();
- public ProcessingStep1()
- {
- lookupTable.Add(typeof(ItemA), ProcessItemA);
- lookupTable.Add(typeof(ItemB), ProcessItemB);
- // ...
- }
- public method CalculateAThing(IEnumerable<TheBaseClass> items)
- {
- foreach (TheBaseClass item in items)
- {
- // desired to throw an exception if the key is not found
- CalculateDelegate calculate = lookupTable[item.GetType()];
- }
- }
- public ProcessItemA(TheBaseClass item)
- {
- // need to cast to access a property from the derived type
- var something = (item as ItemA).PropertyOnItemA;
- item.BaseClassProperty = doMath(something);
- }
- public ProcessItemB(TheBaseClass item)
- {
- // no need to cast, but the behavior is different for ItemB than others
- item.BaseClassProperty = somethingSpecific;
- }
- // ...
- }
- public abstract class TheBaseClass
- {
- public abstract void CalculateAThing();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement