综述
1.调试时的手动类型转换,正如LachlanG和Ken指出的那样
2.利用自Delphi 2010以来引入的Debugger Visualizers概念
3.切换到泛型同行。
=========================================
以下面的代码为例:
如果断点分别设置在TestRegular
的末尾和TestGenerics
的末尾,则可以通过以下方式查看通用列表的项目(甚至是项目的内容)调试检查器,但是当将鼠标悬停在tmp
变量上时,常规tobjectlist没有任何意义(甚至不计数)。我想知道是否有某种方法可以为常规的ashjectlist实现类似的调试时功能?
unit Unit2;
interface
uses
Contnrs, Generics.Collections,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TMyItem = class;
TMyItemList = class;
TForm2 = class;
TMyItem = class
private
fname: string;
public
property name: string read fname;
constructor Create(aName: string);
end;
TMyItemList = class(TObjectList)
protected
procedure SetObject (Index: Integer; Item: TMyItem);
function GetObject (Index: Integer): TMyItem;
public
function Add (Obj: TMyItem): Integer;
procedure Insert (Index: Integer; Obj: TMyItem);
property Objects [Index: Integer]: TMyItem
read GetObject write SetObject; default;
end;
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure TestRegular;
procedure TestGenerics;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TMyItem }
constructor TMyItem.Create(aName: string);
begin
fname := aName;
end;
{ TMyItemList }
function TMyItemList.Add(Obj: TMyItem): Integer;
begin
Result := inherited Add (Obj);
end;
procedure TMyItemList.SetObject(Index: Integer; Item: TMyItem);
begin
inherited SetItem (Index, Item);
end;
function TMyItemList.GetObject(Index: Integer): TMyItem;
begin
Result := inherited GetItem (Index) as TMyItem;
end;
procedure TMyItemList.Insert(Index: Integer; Obj: TMyItem);
begin
inherited Insert(Index, Obj);
end;
{TForm2}
procedure TForm2.FormCreate(Sender: TObject);
begin
TestGenerics;
TestRegular;
end;
procedure TForm2.TestRegular;
var
tmp: TMyItemList;
begin
tmp := TMyItemList.Create;
tmp.Add(TMyItem.Create('1'));
tmp.Add(TMyItem.Create('2'));
tmp.Free;
end;
procedure TForm2.TestGenerics;
var
tmp: TObjectList<TMyItem>;
begin
tmp := TObjectList<TMyItem>.Create;
tmp.Add(TMyItem.Create('1'));
tmp.Add(TMyItem.Create('2'));
tmp.Free;
end;
end.
答案 0 :(得分:6)
我认为您无法改进鼠标光标悬停提示中显示的内容。
然而,您可以在源代码中使用调试窗口内的类型转换。
例如,您可以从评估窗口(Ctrl F7)中将tmp变量强制转换为TObjectList(tmp),或者在类型转换变量上创建监视(Ctrl F5)。
答案 1 :(得分:1)
有Debugger Visualizers允许您自定义调试器的可视化功能。我从来没有使用它们,但我的理解是你可以将它们与一些RTTI结合起来,并提供有关TObject
实例的更丰富的信息。
但是,使用泛型是你想要的。它提供了编译时键入,具有明显的优点。我只是这样做。