#1 2012-11-19 15:19:52

lele9
Member
Registered: 2011-10-28
Posts: 170

compare object

hi,
i need to compare object for identity.
for identity i mean that if all properties of two object, these object are identity.
i think about using rtti with something like this:

function CompareObject(Object1, Object2: TObject): Boolean;
var aContext: TRttiContext;
    aRtti: TRttiType;
    aProperty: TRttiProperty;
    Equality: Boolean;
begin
  Equality := False;
  if (Object1.ClassType = Object2.ClassType) then
  begin
    aContext := TRttiContext.Create;
    try
      aRtti := aContext.GetType(Object1.ClassType);
      for aProperty in aRtti.GetProperties do
      begin
        //THIS NOT WORK. HOW TO COMPARE TWO TVALUE?
        if (aProperty.GetValue(Object1) = aProperty.GetValue(Object2))
        then
          Equality := True
        else
          Equality := False;
      end;
    finally
      aContext.Free;
    end;
  end;
  Result := Equality;
end;

Is this a correct way or there is more simple one?
How can compare two TValue?
Maybe i must to cast TValue to his base type and then compare?

all suggestions are welcome!
thanks!
Emanuele

Offline

#2 2012-11-20 10:48:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: compare object

You can try ObjectToJSON() then compare the serialized values.

Or use the mORMot RTTI and use TPropInfo.SameValue().

Offline

Board footer

Powered by FluxBB