Hi
Hi,
say I've got the following classes:
TClassA = class
end;
TClassB = class
fCA: TClassA;
end;
Now, suppose I do not know that fCA is TClassA but I know that the field is called "fCA" and I want to get access to the instance fCA.
I try this:
//////////////////////
var
ctx: TRTTIContext;
cType: TRTTIType;
clB: TClassB;
cField: TRTTIField;
obj: TObject;
begin
[initialise clB and fCA]
....
cType:=ctx.GetType(clB.ClassInfo);
cField:=cType.GetField('fCA');
if assigned(cField) then
begin
///Q1: How do I check that fCA is TObject??
//I try this but I get AV
obj:=cField.GetValue(cField).AsObject;
//I have also tried this and AV again
obj:=cField.InitInstance(cField.ClassInfo);
end;
//////////////////////
Any ideas?
Thanks a lot in advance
say I've got the following classes:
TClassA = class
end;
TClassB = class
fCA: TClassA;
end;
Now, suppose I do not know that fCA is TClassA but I know that the field is called "fCA" and I want to get access to the instance fCA.
I try this:
//////////////////////
var
ctx: TRTTIContext;
cType: TRTTIType;
clB: TClassB;
cField: TRTTIField;
obj: TObject;
begin
[initialise clB and fCA]
....
cType:=ctx.GetType(clB.ClassInfo);
cField:=cType.GetField('fCA');
if assigned(cField) then
begin
///Q1: How do I check that fCA is TObject??
//I try this but I get AV
obj:=cField.GetValue(cField).AsObject;
//I have also tried this and AV again
obj:=cField.InitInstance(cField.ClassInfo);
end;
//////////////////////
Any ideas?
Thanks a lot in advance
V := cField.GetValue(clB);
ReplyDeleteif V.TypeInfo.Kind = tkClass then ...
Q1: if cField.FieldType.IsInstance then...
ReplyDeleteand
obj := cfield.GetValue(clb).AsObject;