Delphi Can

Orjinalini görmek için tıklayınız: TValue Parameter
Şu anda (Arşiv) modunu görüntülemektesiniz. Orjinal Sürümü Görüntüle internal link
type 

  TPerson = class
  private
   FFirstName : string;
   FLastName : string;
   FAge : Byte;
   FSalary : Double;
  published
   property FirstName: string read FFirstName write FFirstName;
   property LastName: string read FLastName write FLastName;
   property Age: Byte read FAge write FAge;
   property Salary: double read FSalary write FSalary;
  end;

  TPersonArray = array of TPerson;


  TPoint = class
  private
   FX:Integer;
   FY:Integer;
  published
   property X: Integer read FX write FX;
   property Y: Integer read FY write FY;
  end;

  TPointArray = array of TPoint;


procedure DoIt(const AValue : TValue);
begin
  // to do
end;


Yukarıdaki DoIt procedure'üne parametre olarak TPersonArray ve TPointArray parametre olarak geçilip, procedure içinde sınıf verileri (FirstName,LastName , X , Y vs) okunması isteniyor.

procedure'ün gövdesinde, parametre ile geçilen sınıflara bağımlı olmadan okunmalı.Yani procedure içinde TPersonArray, TPointArray,  TPerson, TPoint vs bu sınıflar geçmemeli.
procedure TForm1.BtnTestClick(Sender: TObject);
Var
 xPersonArray : TPersonArray;
 xPointArray  : TPointArray;
 PersonValue  : TValue;
 PointValue   : TValue;
begin
 SetLength(xPersonArray,2);
 xPersonArray[0]            := TPerson.Create;
 xPersonArray[0].FFirstName := 'S';
 xPersonArray[0].FLastName  := 'B';
 xPersonArray[0].FAge       := 25;
 xPersonArray[0].FSalary    := 5000;
 xPersonArray[1]            := TPerson.Create;
 xPersonArray[1].FFirstName := 'X';
 xPersonArray[1].FLastName  := 'Z';
 xPersonArray[1].FAge       := 55;
 xPersonArray[1].FSalary    := 12500;
 PersonValue := TValue.From<TPersonArray>(xPersonArray);
 DoIt(PersonValue);
 SetLength(xPointArray,2);
 xPointArray[0]   := TPoint.Create;
 xPointArray[0].X := 50;
 xPointArray[0].Y := 60;
 xPointArray[1]   := TPoint.Create;
 xPointArray[1].X := 70;
 xPointArray[1].Y := 80;
 PointValue := TValue.From<TPointArray>(xPointArray);
 DoIt(PointValue);
end;

procedure TForm1.DoIt(const AValue: TValue);
Var
 Ind          : Integer;
 xPersonArray : TPersonArray;
 xPointArray  : TPointArray;
begin
 if AValue.IsType<TPersonArray> then
 begin
   xPersonArray := AValue.AsType<TPersonArray>;
   for Ind := Low(xPersonArray) to High(xPersonArray) do
   begin
     MemWrite.Lines.Add(xPersonArray[Ind].FFirstName);
     MemWrite.Lines.Add(xPersonArray[Ind].FLastName);
     MemWrite.Lines.Add(xPersonArray[Ind].FAge.ToString);
     MemWrite.Lines.Add(xPersonArray[Ind].FSalary.ToString);
   end;
 end
 else if AValue.IsType<TPointArray> then
 begin
   xPointArray := AValue.AsType<TPointArray>;
   for Ind := Low(xPointArray) to High(xPointArray) do
   begin
     MemWrite.Lines.Add(xPointArray[Ind].X.ToString);
     MemWrite.Lines.Add(xPointArray[Ind].Y.ToString);
   end;
 end
end;
(16-02-2020, Saat: 23:23)narkotik Adlı Kullanıcıdan Alıntı: [ -> ]
procedure TForm1.BtnTestClick(Sender: TObject);
Var
 xPersonArray : TPersonArray;
 xPointArray  : TPointArray;
 PersonValue  : TValue;
 PointValue   : TValue;
begin
 SetLength(xPersonArray,2);
 xPersonArray[0]            := TPerson.Create;
 xPersonArray[0].FFirstName := 'S';
 xPersonArray[0].FLastName  := 'B';
 xPersonArray[0].FAge       := 25;
 xPersonArray[0].FSalary    := 5000;
 xPersonArray[1]            := TPerson.Create;
 xPersonArray[1].FFirstName := 'X';
 xPersonArray[1].FLastName  := 'Z';
 xPersonArray[1].FAge       := 55;
 xPersonArray[1].FSalary    := 12500;
 PersonValue := TValue.From<TPersonArray>(xPersonArray);
 DoIt(PersonValue);
 SetLength(xPointArray,2);
 xPointArray[0]   := TPoint.Create;
 xPointArray[0].X := 50;
 xPointArray[0].Y := 60;
 xPointArray[1]   := TPoint.Create;
 xPointArray[1].X := 70;
 xPointArray[1].Y := 80;
 PointValue := TValue.From<TPointArray>(xPointArray);
 DoIt(PointValue);
end;

procedure TForm1.DoIt(const AValue: TValue);
Var
 Ind          : Integer;
 xPersonArray : TPersonArray;
 xPointArray  : TPointArray;
begin
 if AValue.IsType<TPersonArray> then
 begin
   xPersonArray := AValue.AsType<TPersonArray>;
   for Ind := Low(xPersonArray) to High(xPersonArray) do
   begin
     MemWrite.Lines.Add(xPersonArray[Ind].FFirstName);
     MemWrite.Lines.Add(xPersonArray[Ind].FLastName);
     MemWrite.Lines.Add(xPersonArray[Ind].FAge.ToString);
     MemWrite.Lines.Add(xPersonArray[Ind].FSalary.ToString);
   end;
 end
 else if AValue.IsType<TPointArray> then
 begin
   xPointArray := AValue.AsType<TPointArray>;
   for Ind := Low(xPointArray) to High(xPointArray) do
   begin
     MemWrite.Lines.Add(xPointArray[Ind].X.ToString);
     MemWrite.Lines.Add(xPointArray[Ind].Y.ToString);
   end;
 end
end;

procedure'ün gövdesinde, parametre ile geçilen sınıflara bağımlı olmadan okunmalı.Yani procedure içinde TPersonArray, TPointArray,  TPerson, TPoint bunlar geçmeyecek Smile
procedure TForm1.DoIt(const AValue: TValue);
Var
 Ind        : Integer;
 xRttiType  : TRttiType;
 xRttiField : TRttiField;
begin
 for Ind := 0 to Pred(AValue.GetArrayLength) do
 begin
   xRttiType := TRttiContext.Create.GetType(AValue.GetArrayElement(Ind).TypeInfo);
   for xRttiField in xRttiType.GetFields do
     MemWrite.Lines.Add(Format('Index = %d FieldName=%s Value=%s',[Ind,xRttiField.Name,xRttiField.GetValue(AValue.GetArrayElement(Ind).AsObject).AsVariant]));
 end;
end;
Şöyle düzelteyim o zaman Smile
(17-02-2020, Saat: 00:57)narkotik Adlı Kullanıcıdan Alıntı: [ -> ]
procedure TForm1.DoIt(const AValue: TValue);
Var
 Ind        : Integer;
 xRttiType  : TRttiType;
 xRttiField : TRttiField;
begin
 for Ind := 0 to Pred(AValue.GetArrayLength) do
 begin
   xRttiType := TRttiContext.Create.GetType(AValue.GetArrayElement(Ind).TypeInfo);
   for xRttiField in xRttiType.GetFields do
     MemWrite.Lines.Add(Format('Index = %d FieldName=%s Value=%s',[Ind,xRttiField.Name,xRttiField.GetValue(AValue.GetArrayElement(Ind).AsObject).AsVariant]));
 end;
end;
Şöyle düzelteyim o zaman Smile
Ellerinize sağlık. Smile

Döngü içinde sürekli, TRttiContext.Create.GetType çağırımlarını yapmak yerine, döngü başında bir kez hafızada tahsis edip, döngü içinde GetType çağırsak daha performanslı olabilir.
var
 Context : TRttiContext;
begin
 Context := TRttiContext.Create;
 for .....
    Context.GetType....


Bu sorunun amacı;

Tür (Type) bağımsız kodlamayı ve TValue yapısı ile neler yapılabileceğini izah etmek.
Bir önceki kodlamaya göre karşılaştırıldığında, DoIt fonksiyonu belirtilen tiplere(TPersonArray vs) bağımlı kodlanmıştı.Ve taşınamaz durumdaydı. 
Ya da bir başka yerde kullanmak istersek, fonksiyon içinde kullanılan türleri (type) de  taşımamız gerekiyordu ve kırılgan bir yapıya bir sahip olacaktı.