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
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
Any help would be greatly appreciated!
Thanks
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.
ReplyDeleteLet'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...
http://docwiki.embarcadero.com/Libraries/Berlin/en/System.Rtti.TValue.Make
ReplyDeleteStefan Glienke that was my first try but got no success:
ReplyDeletefunction 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).
Pass nil to ABuffer
ReplyDeleteStefan 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.
ReplyDeleteI owe you (also this) one ! ;-)