ex6.pp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. program example6;
  2. { This program demonstrates the GetMethodProp function }
  3. {$mode objfpc}
  4. uses rttiobj,typinfo,sysutils;
  5. Type
  6. TNotifyObject = Class(TObject)
  7. Procedure Notification1(Sender : TObject);
  8. Procedure Notification2(Sender : TObject);
  9. end;
  10. Procedure TNotifyObject.Notification1(Sender : TObject);
  11. begin
  12. Write('Received notification 1 of object with class: ');
  13. Writeln(Sender.ClassName);
  14. end;
  15. Procedure TNotifyObject.Notification2(Sender : TObject);
  16. begin
  17. Write('Received notification 2 of object with class: ');
  18. Writeln(Sender.ClassName);
  19. end;
  20. Var
  21. O : TMyTestObject;
  22. PI : PPropInfo;
  23. NO : TNotifyObject;
  24. M : TMethod;
  25. Procedure PrintMethod (Const M : TMethod);
  26. begin
  27. If (M.Data=Pointer(NO)) Then
  28. If (M.Code=Pointer(@TNotifyObject.Notification1)) then
  29. Writeln('Notification1')
  30. else If (M.Code=Pointer(@TNotifyObject.Notification2)) then
  31. Writeln('Notification2')
  32. else
  33. begin
  34. Write('Unknown method adress (data:');
  35. Write(hexStr(Longint(M.data),8));
  36. Writeln(',code:',hexstr(Longint(M.Code),8),')');
  37. end;
  38. end;
  39. begin
  40. O:=TMyTestObject.Create;
  41. NO:=TNotifyObject.Create;
  42. O.NotifyEvent:[email protected];
  43. PI:=GetPropInfo(O,'NotifyEvent');
  44. Writeln('Method property : ');
  45. Write('Notifying : ');
  46. O.Notify;
  47. Write('Get (name) : ');
  48. M:=GetMethodProp(O,'NotifyEvent');
  49. PrintMethod(M);
  50. Write('Notifying : ');
  51. O.Notify;
  52. Write('Get (propinfo) : ');
  53. M:=GetMethodProp(O,PI);
  54. PrintMethod(M);
  55. M:=TMethod(@NO.Notification2);
  56. SetMethodProp(O,'NotifyEvent',M);
  57. Write('Set (name,Notification2) : ');
  58. M:=GetMethodProp(O,PI);
  59. PrintMethod(M);
  60. Write('Notifying : ');
  61. O.Notify;
  62. Write('Set (propinfo,Notification1) : ');
  63. M:=TMethod(@NO.Notification1);
  64. SetMethodProp(O,PI,M);
  65. M:=GetMethodProp(O,PI);
  66. PrintMethod(M);
  67. Write('Notifying : ');
  68. O.Notify;
  69. O.Free;
  70. end.