RTTI 动态数组 TValue Delphi 2010

发布于 2024-10-09 20:43:53 字数 619 浏览 5 评论 0 原文

我有一个问题。我是来自 Delphi 2010 的运行时类型信息的新手。我需要将动态数组的长度设置为 TValue。你可以看到代码。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳絮泡泡 2024-10-16 20:43:53

TValue 并不是为任意操作其内容而设计的(如果是的话,它将有更多的帮助器,例如设置记录字段等),而是为在具体静态类型和动态 RTTI 之间传输值而设计。在这方面,TValue.SetArrayElement 是一种异常现象,事后看来,也许不应该包含在内。然而,你问的是可能的:

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文