5

I have a question. I am a newbie with Run Time Type Information from Delphi 2010. I need to set length to a dynamic array into a TValue. You can see the code.

Type TMyArray = array of integer;
TMyClass = class
publihed
function Do:TMyArray;
end;

function TMyClass.Do:TMyArray;
begin
SetLength(Result,5);
for i:=0 to 4 Result[i]=3;
end;
.......
.......
......
y:TValue;
Param:array of TValue;
.........
y=Methods[i].Invoke(Obj,Param);//delphi give me a DynArray type kind, is working, Param works to any functions.

if Method[i].ReturnType.TypeKind = tkDynArray then//is working...
begin
    I want to set length for y to 10000//i don't know how to write.
end;

I don't like Generics Collections.

2
  • 5
    why don't you like the classes in Generics.Collections? Commented Dec 30, 2010 at 10:11
  • 6
    Lack of generics was the biggest reason for me to drop Delphi, and not that they are available you don't use them? They are probably the biggest improvement to Delphi since Delphi 2 or so. Commented Dec 30, 2010 at 10:42

1 Answer 1

8

TValue wasn't designed for arbitrary manipulation of its contents (it would have more helpers for e.g. setting record fields etc. if so), but rather for transporting values between concrete static types and dynamic RTTI. In this respect, TValue.SetArrayElement is an anomaly, and in hindsight, perhaps should not have been included. However, what you ask is possible:

uses Rtti;

type
  TMyArray = array of Integer;
  TMyClass = class
    function Go: TMyArray;
  end;

function TMyClass.Go: TMyArray;
var
  i: Integer;
begin
  SetLength(Result, 5);
  for i := 0 to 4 do
    Result[i] := 3;
end;

procedure P;
var
  ctx: TRttiContext;
  v: TValue;
  len: Longint;
  i: Integer;
begin
  v := ctx.GetType(TMyClass).GetMethod('Go').Invoke(TMyClass.Create, []);
  Writeln(v.ToString);
  len := 10;
  DynArraySetLength(PPointer(v.GetReferenceToRawData)^, v.TypeInfo, 1, @len);
  Writeln(v.GetArrayLength);
  for i := 0 to v.GetArrayLength - 1 do
    Writeln(v.GetArrayElement(i).ToString);
end;

begin
  P;
end.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.