Advertisement
Guest User

Untitled

a guest
May 29th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DynamicPlayground
  4. {
  5. class Program
  6. {
  7. /// <summary>
  8. /// When this is private it fails with:
  9. /// An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll
  10. /// Additional information: 'DynamicPlayground.BaseHandler' does not contain a definition for 'When'
  11. /// </summary>
  12. private class TestHandler : BaseHandler
  13. {
  14. public bool Handled { get; private set; }
  15.  
  16. public void When(TestEvent ev)
  17. {
  18. Handled = true;
  19. }
  20. }
  21.  
  22. static void Main(string[] args)
  23. {
  24.  
  25. var ev = new TestEvent();
  26. var sut = new TestHandler();
  27. sut.Handle(ev);
  28.  
  29. Console.WriteLine("Handled: " + sut.Handled);
  30.  
  31. Console.ReadKey();
  32. }
  33. }
  34.  
  35. public abstract class BaseHandler
  36. {
  37. public void Handle(DomainEvent ev)
  38. {
  39. dynamic dynamicThis = (dynamic)this;
  40. dynamicThis.When((dynamic)ev);
  41. }
  42. }
  43.  
  44. public class TestEvent : DomainEvent { }
  45. public abstract class DomainEvent { }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement