[fpc-pascal] RTTI and generics: accessing specialized class instance

leledumbo leledumbo_cool at yahoo.co.id
Fri Oct 30 20:10:44 CET 2015


Consider the following:

type
  generic TGenClass<T> = class
  ...
  end;

  TSpecType = specialize TGenClass<Integer>;

  TMyClass = class
    FST: TSpecType;
    procedure p;
  published
    ST: TSpecType read FST write FST;
  end;

procedure TMyClass.p;
...
begin
  PropCount := GetPropList(Self,Props);
  for i := 0 to PropCount - 1 do begin
    Prop := Props^[i];
    PropName := Prop^.Name;
    case Prop^.PropType^.Kind of
      tkObject: begin
        Obj := GetObjectProp(Self,Prop); //1
        ...
      end;
    end;
  end;
end;

In //1, GetObjectProp will return TObject for ST property. Since generic
types without specialization cannot be used in type declaration, how can I
get the actual type of ST and cast Obj to it so I can access its properties?
Note that ST here is just an example, there could be multiple TGenClass
specialization that TMyClass uses. 



--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/RTTI-and-generics-accessing-specialized-class-instance-tp5723011.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.



More information about the fpc-pascal mailing list