如何在运行时设置数组长度?setLength(t.GetProperty( 'Propertys'),3); ????
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TSubProperty = Class
private
Fitem2: Integer;
Fitem1: String;
procedure Setitem1(const Value: String);
procedure Setitem2(const Value: Integer);
published
property item1:String read Fitem1 write Setitem1;
property item2:Integer read Fitem2 write Setitem2;
End;
TArraySubPropertys=array of TSubProperty;
TmyObject = Class
private
FPropertys: TArraySubPropertys;
procedure SetPropertys(const Value: TArraySubPropertys);
published
property Propertys:TArraySubPropertys read FPropertys write SetPropertys;
End;
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private …Run Code Online (Sandbox Code Playgroud) 根据之前一篇文章的一个答案,我正在研究以下设计的可能性
TChildClass = class(TObject)
private
FField1: string;
FField2: string;
end;
TMyClass = class(TObject)
private
FField1: TChildClass;
FField2: TObjectList<TChildClass>;
end;
Run Code Online (Sandbox Code Playgroud)
现在,在现实世界中,TMyClass将有10个不同的列表,所以我希望能够使用RTTI解决这些列表.但是,我对这个类的其他字段不感兴趣,所以我需要检查某个字段是否是某种TObjectList.这是我到目前为止所得到的:
procedure InitializeClass(RContext: TRttiContext; AObject: TObject);
var
ROwnerType: TRttiType;
RObjListType: TRttiType;
RField: TRttiField;
SchInf: TSchemaInfoDetail;
begin
ROwnerType := RContext.GetType(AObject.ClassInfo);
RObjListType := RContext.GetType(TObjectList<TObject>);
for RField in ROwnerType.GetFields do begin
// How do I check if the type of TMyClass.FField2 (which is TObjectList<TChildClass>) is some sort of TObjectList?
end;
Run Code Online (Sandbox Code Playgroud)
显然,RField.FieldType <> RObjListType.FieldType.但是,他们确实有一些关系,不是吗?看起来很糟糕(而且错误!)对常见功能进行非常精细的检查,以使其非常可能RField.FieldType实际上是一个TObjectList.
说实话,我对泛型非常不舒服,所以这个问题可能很天真.但是,我非常乐意学习.以上解决方案是否可以实施?TIA!