ex6.pp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.Data:=No;
  56. M.Code:=Pointer(@NO.Notification2);
  57. SetMethodProp(O,'NotifyEvent',M);
  58. Write('Set (name,Notification2) : ');
  59. M:=GetMethodProp(O,PI);
  60. PrintMethod(M);
  61. Write('Notifying : ');
  62. O.Notify;
  63. Write('Set (propinfo,Notification1) : ');
  64. M.Data:=No;
  65. M.Code:=Pointer(@NO.Notification1);
  66. SetMethodProp(O,PI,M);
  67. M:=GetMethodProp(O,PI);
  68. PrintMethod(M);
  69. Write('Notifying : ');
  70. O.Notify;
  71. O.Free;
  72. end.