![]() |
|
TValue Parameter - Baskı Önizleme +- Delphi Can (https://www.delphican.com) +-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3) +--- Forum: Access violation (https://www.delphican.com/forumdisplay.php?fid=136) +--- Konu Başlığı: TValue Parameter (/showthread.php?tid=4532) |
TValue Parameter - ismailkocacan - 16-02-2020 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. Cvp: TValue Parameter - narkotik - 16-02-2020 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; Cvp: TValue Parameter - ismailkocacan - 16-02-2020 (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
Cvp: TValue Parameter - narkotik - 17-02-2020 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
Cvp: TValue Parameter - ismailkocacan - 17-02-2020 (17-02-2020, Saat: 00:57)narkotik Adlı Kullanıcıdan Alıntı:Ellerinize sağlık. ![]() 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ı. |