Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Citanje vrednosti peoperty-ja uz pomoc RTTI

[es] :: Pascal / Delphi / Kylix :: Citanje vrednosti peoperty-ja uz pomoc RTTI

[ Pregleda: 2628 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

looping
none
none

Član broj: 324206
Poruke: 18
95.180.97.*



Profil

icon Citanje vrednosti peoperty-ja uz pomoc RTTI04.08.2014. u 14:54 - pre 117 meseci
Pozdrav,
Koristim RTTI da bih u RunTime-u dobio sve property-e nekog objekta i to znam kako da uradim za property-e prostih tipova (string, integer ...),
medjutim, ako imam property tipa neke klase i zelim da dobijem vrednost nekog polja te klase (na pr. TStringList tip polja) onda ne mogu nista da dobijem.
Da li neko ima ideju kako da dobijem vrednost slozenog propertija?
 
Odgovor na temu

reiser

Član broj: 7895
Poruke: 2314



+102 Profil

icon Re: Citanje vrednosti peoperty-ja uz pomoc RTTI04.08.2014. u 15:54 - pre 117 meseci
Imas vec gotovo resenje: http://code.google.com/p/delphi-oop/wiki/SvSerializer

A ako hoces sam:

Code:

{$APPTYPE CONSOLE}

uses
  System.Classes, System.Rtti, System.TypInfo;

procedure PrintStringList(const AObject: TObject);
var
  t: TRttiType;
  p: TRttiProperty;
  m: TRttiMethod;
  C1, count: Integer;
begin
  t := TRttiContext.Create.GetType(AObject.ClassType);
  p := t.GetProperty('Count');
  m := t.GetMethod('Get');
  if (Assigned(p)) and (Assigned(m)) then
  begin
    count := p.GetValue(AObject).AsInteger;
    for C1 := 0 to count - 1 do
      WriteLn(m.Invoke(AObject, [C1]).ToString);
  end;
end;

var
  sl: TStringList;
begin
  sl := TStringList.Create;
  try
    sl.Add('text 1');
    sl.Add('text 3');
    sl.Add('text 2');
    PrintStringList(sl);
  finally
    sl.Free;
  end;
end.


Nisam siguran gde ti skripi, tako da je najbolje da postujes specificniji problem ili kod, pa ces dobiti i bolju pomoc.
 
Odgovor na temu

looping
none
none

Član broj: 324206
Poruke: 18
95.180.97.*



Profil

icon Re: Citanje vrednosti peoperty-ja uz pomoc RTTI04.08.2014. u 16:48 - pre 117 meseci
Mozda sam bio malo nejasan, jer je problem u ugnjezdenom propertiju,
na pr
Code:

myContrl.SomeProperty.myValue

Kako da dodjem do myValue?
 
Odgovor na temu

reiser

Član broj: 7895
Poruke: 2314



+102 Profil

icon Re: Citanje vrednosti peoperty-ja uz pomoc RTTI04.08.2014. u 16:54 - pre 117 meseci
Sa GetProperty() dobijas property, sa GetMethod() dobijas metodu, ako hoces da dobijes vrednost propertija, onda pozivas p.As*, gde je p TRttiProperty koji si prethodno dobio preko GetProperty(). Pogledaj kod iznad koji sam postovao, tu imas sve sto ti treba.
 
Odgovor na temu

looping
none
none

Član broj: 324206
Poruke: 18
95.180.97.*



Profil

icon Re: Citanje vrednosti peoperty-ja uz pomoc RTTI04.08.2014. u 19:38 - pre 117 meseci
Konkretan problem je sledeci:
Imam kontrolu tipa TStringGridExtended koja je nasledjena od kontrole TStringGrid
Kontrola ima property CustomEdits (tipa TCustomEdits koja nasledjuje od TPersistent klase) i sadrzi property-e Names tipa TStringList, Columns tipa TFilterDropDownColumns ...

Code:

r := ctx.GetType(TStringList);
p := r.GetProperty('Count');
m := r.GetMethod('Get');
count := p.GetValue((myStringGrid)).AsInteger; //Ovo pukne jer ne znam kako da prosledim konkretan objekat (TStringList) a ne celu kontrolu


 
Odgovor na temu

reiser

Član broj: 7895
Poruke: 2314



+102 Profil

icon Re: Citanje vrednosti peoperty-ja uz pomoc RTTI04.08.2014. u 23:31 - pre 117 meseci
TStringList nadjes na slican nacin kao i propertije/metode:

Code:

{$APPTYPE CONSOLE}

uses
  System.Classes, Vcl.Grids, System.Rtti, System.TypInfo;

type
  TCustomEdits = class(TPersistent)
  private
    FNames: TStringList;
  public
    constructor Create;
    destructor Destroy; override;
    property Names: TStringList read FNames;
  end;

  TStringGridExtended = class(TStringGrid)
  private
    FCustomEdits: TCustomEdits;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;

    property CustomEdits: TCustomEdits read FCustomEdits;
  end;

{ TStringGridExtended }

constructor TStringGridExtended.Create;
begin
  inherited Create(nil);
  FCustomEdits := TCustomEdits.Create;
end;

destructor TStringGridExtended.Destroy;
begin
  FCustomEdits.Free;
  inherited;
end;

{ TCustomEdits }

constructor TCustomEdits.Create;
begin
  FNames := TStringList.Create;
end;

destructor TCustomEdits.Destroy;
begin
  FNames.Free;
  inherited;
end;

procedure PrintStringList(const AObject: TObject);
 var
   t: TRttiType;
   p: TRttiProperty;
   m: TRttiMethod;
   C1, count: Integer;
 begin
   t := TRttiContext.Create.GetType(AObject.ClassType);
   p := t.GetProperty('Count');
   m := t.GetMethod('Get');
   if (Assigned(p)) and (Assigned(m)) then
   begin
     count := p.GetValue(AObject).AsInteger;
     for C1 := 0 to count - 1 do
       WriteLn(m.Invoke(AObject, [C1]).ToString);
   end;
 end;

var
  sge: TStringGridExtended;
  c: TRttiContext;
  p1, p2: TRttiProperty;
  v1, v2: TValue;
begin
  sge := TStringGridExtended.Create;
  try
    sge.CustomEdits.Names.Add('text 1');
    sge.CustomEdits.Names.Add('text 3');
    sge.CustomEdits.Names.Add('text 2');

    c := TRttiContext.Create;
    p1 := c.GetType(sge.ClassType).GetProperty('CustomEdits');
    if Assigned(p1) then
    begin
      p2 := c.GetType(p1.PropertyType.Handle).GetProperty('Names');
      if Assigned(p2) then
      begin
        v1 := p1.GetValue(sge);
        v2 := p2.GetValue(v1.AsObject);
        PrintStringList(v2.AsObject);
      end;
    end;
  finally
    sge.Free;
  end;

  ReadLn;
end.


Referenca: http://docwiki.embarcadero.com/Libraries/XE3/en/System.Rtti
 
Odgovor na temu

looping
none
none

Član broj: 324206
Poruke: 18
95.180.97.*



Profil

icon Re: Citanje vrednosti peoperty-ja uz pomoc RTTI05.08.2014. u 10:42 - pre 117 meseci
Hvala na pomoci, uspeo sam uz pomoc prikazanog koda
 
Odgovor na temu

[es] :: Pascal / Delphi / Kylix :: Citanje vrednosti peoperty-ja uz pomoc RTTI

[ Pregleda: 2628 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.