Einzelnen Beitrag anzeigen

Keldorn

Registriert seit: 6. Mär 2003
Ort: Meißen
876 Beiträge
 
Delphi 10.1 Berlin Professional
 
#9

AW: Generics-Typparameter einer TObjectlist herausfinden

  Alt 19. Mär 2012, 20:01
Hallo,

Die letzten Beiträge habe ich noch nicht ganz verstanden , das muß ich mir nochmal in Ruhe durchdenken.

Mir war das mit dem Syntax nicht ganz klar: mir hat das procedure DisplayList<T: class>(AList: TObjectList<T>); gefehlt.
Habe erstmal was funktionierendes mit Hilfe von anderen DP-Threads:

Testcode:
Delphi-Quellcode:
procedure TForm1.Proc_InsGridUebertragenDP<T>(AList: TObjectList<T>);
Var prop: TRttiProperty;
    proparr:TArray<TRttiProperty>;
    value: TValue;
    Context: TRttiContext;
    obj:Tobject;
    io,ip,acol:integer;
begin
  StringGrid1.RowCount:=max(2,
                            AList.Count);

  //Überschriften
  StringGrid1.ColCount:=Length(Context.GetType(system.TypeInfo(T)).GetProperties);
  acol:=0;
  for prop in Context.GetType(system.TypeInfo(T)).GetProperties do
    begin
      StringGrid1.Cells[acol,0]:=prop.Name;
      inc(acol);
    end;

  //Inhalt
  for io := 0 to AList.Count-1 do
    begin
      obj:=AList[io];
      proparr:=Context.GetType(obj.ClassType).GetProperties;
      for ip:=low(proparr) to high(proparr) do
        begin
          prop:=proparr[ip];
          Value:=prop.GetValue(obj);

          case Prop.PropertyType.TypeKind of
             tkInteger:
               begin
                 Proc_Info(prop.name+'"Integer"'+value.ToString);
                 StringGrid1.Cells[ip,io+1]:=inttostr(Value.AsInteger);
               end;
             tkFloat:
               begin
                 Proc_Info(prop.name+'"Float"'+value.ToString);
                 StringGrid1.Cells[ip,io+1]:=FloatToStr(Value.AsExtended);
               end;
             tkString,tkUstring:
               begin
                 Proc_Info(prop.name+'"String"'+value.ToString);
                 StringGrid1.Cells[ip,io+1]:=value.AsString;
               end;
            else
              showmessage('unbekannt');
           end;
      end;
    end;
end;
Es würde jetzt das machen, was ich später will -> eine flexible Editiermöglichkeit für eine Liste, ohne das Grid selber aufbauen zu müssen.
Auslesen würde ja auch gehen, da hat mir bei
procedure Proc_AusDemGridLesen<T: class, constructor>(AList: TObjectList<T>) ein anderer Thread mit dem ,constuctor geholfen.

Danke

Lükes Grundlage der Programmierung:
Es wird nicht funktionieren
(Murphy)
  Mit Zitat antworten Zitat