Quick.RTTI.Utils.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. unit Quick.RTTI.Utils;
  2. interface
  3. uses
  4. SysUtils,
  5. Rtti;
  6. type
  7. TRTTI = class
  8. private class var
  9. fCtx : TRttiContext;
  10. public
  11. //class function GetProperties();
  12. class function GetField(aInstance : TObject; const aFieldName : string) : TRttiField; overload;
  13. class function GetField(aTypeInfo : Pointer; const aFieldName : string) : TRttiField; overload;
  14. class function FieldExists(aTypeInfo : Pointer; const aFieldName : string) : Boolean;
  15. class function GetFieldValue(aInstance : TObject; const aFieldName : string) : TValue; overload;
  16. class function GetFieldValue(aTypeInfo : Pointer; const aFieldName: string) : TValue; overload;
  17. class function GetProperty(aInstance : TObject; const aPropertyName : string) : TRttiProperty; overload;
  18. class function GetProperty(aTypeInfo : Pointer; const aPropertyName : string) : TRttiProperty; overload;
  19. class function PropertyExists(aTypeInfo : Pointer; const aPropertyName : string) : Boolean;
  20. class function GetPropertyValue(aInstance : TObject; const aPropertyName : string) : TValue; overload;
  21. class function GetPropertyValue(aTypeInfo : Pointer; const aPropertyName : string) : TValue; overload;
  22. end;
  23. ERTTIError = class(Exception);
  24. implementation
  25. { TRTTIUtils }
  26. class function TRTTI.FieldExists(aTypeInfo: Pointer; const aFieldName: string): Boolean;
  27. var
  28. rtype : TRttiType;
  29. begin
  30. rtype := fCtx.GetType(aTypeInfo);
  31. Result := rtype.GetField(aFieldName) <> nil;
  32. end;
  33. class function TRTTI.GetField(aInstance: TObject; const aFieldName: string): TRttiField;
  34. var
  35. rtype : TRttiType;
  36. begin
  37. rtype := fCtx.GetType(aInstance.ClassInfo);
  38. if rtype <> nil then
  39. begin
  40. Result := rtype.GetField(aFieldName);
  41. end;
  42. end;
  43. class function TRTTI.GetField(aTypeInfo: Pointer; const aFieldName: string): TRttiField;
  44. var
  45. rtype : TRttiType;
  46. begin
  47. rtype := fCtx.GetType(aTypeInfo);
  48. if rtype <> nil then
  49. begin
  50. Result := rtype.GetField(aFieldName);
  51. end;
  52. end;
  53. class function TRTTI.GetFieldValue(aInstance : TObject; const aFieldName: string): TValue;
  54. var
  55. rfield: TRttiField;
  56. begin
  57. rfield := GetField(aInstance,aFieldName);
  58. if rfield <> nil then Result := rfield.GetValue(aInstance);
  59. end;
  60. class function TRTTI.GetFieldValue(aTypeInfo : Pointer; const aFieldName: string): TValue;
  61. var
  62. rfield: TRttiField;
  63. begin
  64. rfield := GetField(aTypeInfo,aFieldName);
  65. if rfield <> nil then rfield.GetValue(aTypeInfo);
  66. end;
  67. class function TRTTI.GetProperty(aInstance: TObject; const aPropertyName: string): TRttiProperty;
  68. var
  69. rtype : TRttiType;
  70. begin
  71. rtype := fCtx.GetType(aInstance.ClassInfo);
  72. if rtype <> nil then Result := rtype.GetProperty(aPropertyName);
  73. end;
  74. class function TRTTI.GetProperty(aTypeInfo: Pointer; const aPropertyName: string): TRttiProperty;
  75. var
  76. rtype : TRttiType;
  77. begin
  78. rtype := fCtx.GetType(aTypeInfo);
  79. if rtype <> nil then Result := rtype.GetProperty(aPropertyName);
  80. end;
  81. class function TRTTI.GetPropertyValue(aInstance: TObject; const aPropertyName: string): TValue;
  82. var
  83. rprop : TRttiProperty;
  84. begin
  85. rprop := GetProperty(aInstance,aPropertyName);
  86. if rprop <> nil then Result := rprop.GetValue(aInstance);
  87. end;
  88. class function TRTTI.GetPropertyValue(aTypeInfo: Pointer; const aPropertyName: string): TValue;
  89. var
  90. rprop : TRttiProperty;
  91. begin
  92. rprop := GetProperty(aTypeInfo,aPropertyName);
  93. if rprop <> nil then Result := rprop.GetValue(aTypeInfo);
  94. end;
  95. class function TRTTI.PropertyExists(aTypeInfo: Pointer; const aPropertyName: string): Boolean;
  96. begin
  97. Result := fCtx.GetType(aTypeInfo).GetProperty(aPropertyName) <> nil;
  98. end;
  99. end.