RTTI动态数组TValue德尔福2010 [英] RTTI Dynamic array TValue Delphi 2010

查看:196
本文介绍了RTTI动态数组TValue德尔福2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。我与运行时类型信息的新手,从2010年德尔福我需要设置长度动态数组成TValue。你可以看到code。

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.

推荐答案

TValue 不是为(它会对如设置更多的帮手其内容任意操纵记录字段等等若有),而是对于混凝土静态类型和动态RTTI之间传输的值。在这方面, TValue.SetArrayElement 是一个异常,并在事后看来,或许不应该被包括在内。但是,你问什么是可能的:

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.

这篇关于RTTI动态数组TValue德尔福2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆