Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. public abstract class SkillBase
  2. {
  3. // Common properties for all skills
  4. }
  5.  
  6. public class WalkSkill : SkillBase
  7. {
  8. // Some properties
  9. }
  10.  
  11. public class RunSkill : SkillBase
  12. {
  13. // Some properties
  14. }
  15.  
  16. public abstract class EditSkillViewModel<T> : ViewModelBase where T : Skill
  17. {
  18. public abstract T Skill { get; protected set; }
  19. }
  20.  
  21. [EditViewModelFor(typeof(WalkSkill))] // Attribute telling that this viewmodel should be instantiated when we want to edit a WalkSkill object
  22. public class EditWalkSkillViewModel : EditSkillViewModel<WalkSkill>
  23. {
  24. public override WalkSkill Skill { get; protected set; }
  25.  
  26. public EditWalkSkillViewModel(WalkSkill skill)
  27. {
  28. Skill = skill;
  29. }
  30. }
  31.  
  32. [EditViewModelFor(typeof(RunSkill))] // Attribute telling that this viewmodel should be instantiated when we want to edit a RunSkill object
  33. public class EditRunSkillViewModel : EditSkillViewModel<RunSkill>
  34. {
  35. public override RunSkill Skill { get; protected set; }
  36.  
  37. public EditRunSkillViewModel(RunSkill skill)
  38. {
  39. Skill = skill;
  40. }
  41. }
  42.  
  43. public static ViewModelBase GetEditSkillViewModel(this Skill skill)
  44. {
  45. var viewModelType = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
  46. from type in assembly.GetTypes()
  47. where
  48. type.IsDefined(typeof(EditViewModelForAttribute)) &&
  49. type.GetCustomAttribute<EditViewModelForAttribute>().SkillType == skill.GetType()
  50. select type).SingleOrDefault();
  51.  
  52. return viewModelType == null ? null : (ViewModelBase)Activator.CreateInstance(viewModelType, skill);
  53. }
  54.  
  55. var editViewModel = selectedSkill.GetEditSkillViewModel();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement