Reputation: 470
I'm working on Delphi 10.2 Tokyo and facing the following situation:
Assuming I have the following procedure, to verify the properties of an object:
procedure TGenericUnit.VerifyProps<T>(_AObj: T);
var
AContext: TRttiContext;
AType: TRttiType;
AProp: TRttiProperty;
begin
AType := AContext.GetType(T);
for AProp in AType.GetProperties do
if AProp.PropertyType is TObject then
// VerifyProps<?>(?);
end;
If I don't know which type of object it will be and it won't necessarily be the same as T, how could I call this procedure recursively?
Upvotes: 0
Views: 498
Reputation: 21758
Do all your work in a non generic method working with TObject
.
You can get the type from AObj.ClassType
or AObj.ClassInfo
passing that to AContext.GetType
.
By the way your check if AProp.PropertyType is TObject then
is wrong because yes of course PropertType is a TObject
(it is an TRttiType
object). What you probably mean is if AProp.PropertyType.IsInstance then
(is the type of the property a class type)
Upvotes: 5
Reputation: 613461
Generics are a compile time construct. The type parameters must be resolved at compile time. Since you don't know the type at compile time you can't call the generic method.
Instead of using a generic method, you will have to implement this using RTTI. Instead of using a generic parameter to specify the type, use a standard parameter of type TRttiType
.
Upvotes: 3