Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.110 Beiträge
 
Delphi 10 Seattle Enterprise
 
#5

AW: Nur veränderte Eigenschaften einer Klasse auslesen?

  Alt 11. Apr 2016, 10:27
Ich habe grade mal das hier:

Delphi-Quellcode:
program Project11;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.Rtti,
  System.Generics.Defaults,
  Delphi.Mocks.Helpers in 'Delphi.Mocks.Helpers.pas';

type
   TFoo = class(TObject)
      public var
         SomeInt:   Integer;
         SomeString:   String;
      public
         function Clone(): TFoo;
   end;

function TFoo.Clone(): TFoo;
begin
   Result := TFoo.Create();
   try
      Result.SomeInt := SomeInt;
      Result.SomeString := SomeString;
   except
      Result.Destroy(); raise;
   end;
end;


procedure printFieldDifference(const a, b: TFoo);
var
   context: TRttiContext;

   fields:   TArray<TRttiField>;
   field:   TRttiField;
begin
   context := TRttiContext.Create();

   fields := context.GetType(TFoo).GetFields();
   for field in fields do
      if not field.GetValue(a).Equals( field.GetValue(b) ) then begin
         WriteLn('Field "', field.Name, '" is different.');
         WriteLn('a: ', field.GetValue(a).ToString());
         WriteLn('b: ', field.GetValue(a).ToString());
      end;


end;


var
   myInstance: TFoo;
   myBackup:   TFoo;
begin
   myInstance := TFoo.Create();
   myInstance.SomeInt := 42;
   myInstance.SomeString := 'Hallo Welt';

   myBackup := myInstance.Clone();

   // Irgendetwas geschieht...
   myInstance.SomeString := 'Tschüss Welt';
   //myInstance.SomeInt := 99;

   printFieldDifference(myInstance, myBackup);

   myInstance.Destroy();
   myBackup.Destroy();

   readln;
end.
(Delphi Mocks @ https://github.com/VSoftTechnologies...ks.Helpers.pas)
Wird benötigt um die TValue-Felder zu vergleichen


Gibt am Schluss aus welche Felder verändert wurden.
Miniaturansicht angehängter Grafiken
unbenannt.png  
  Mit Zitat antworten Zitat