Hi all, I need help! :-)

Hi all, I need help! :-)
I'd like to have a TValue holding a record instance built starting only from a TRttiType (TRttiRecordType, of course).

function TMyClass.ToRecord(const ARecordType: TRttiType): TValue;
begin
Result := ???
end;

I already have a version of this function using generics: function ToRecord: T; but I'd like to have a more general one using only TRttiType.

Any help would be greatly appreciated!

Thanks

Comments

  1. If I have a record instance and put it into a TValue, I suppose it is being copied (correct me if I am wrong!) and not passed by reference.
    Let's say:
    LValue := TValue.From(LMyRecord);
    LMyRecord is on the stack, LValue is on the stack but can be a result value of a function and is being passed by value (not by reference).
    I am trying to do the same but without hard-coding TMyRecord...

    ReplyDelete
  2. Stefan Glienke that was my first try but got no success:

    function TForm1.ToRecord(const ARecordType: TRttiType): TValue;
    var
    LBuffer: Pointer;
    begin
    GetMem(LBuffer, ARecordType.TypeSize);
    TValue.Make(LBuffer, ARecordType.Handle, Result);
    end;

    sometimes AV, sometimes an empty result (but IsEmpty = False)...
    And obviously this is wrong memory management IMHO (I should free memory after having the TValue instance).

    ReplyDelete
  3. Stefan Glienke Many thanks, it worked like a charm! I would have never figured out I could pass nil. Maybe that should be written in the documentation.
    I owe you (also this) one ! ;-)

    ReplyDelete

Post a Comment