5

我已经放弃了 Delphi 7 调试器,并且非常依赖 outputdebugstrings。是否有一个标准函数我可以调用来将对象的内容作为字符串获取,就像我设置断点时调试器一样?

4

3 回答 3

5

不完全是您要查找的内容,但您可以使用 RTTI 访问各种已发布属性的值。神奇的例程在 TypInfo 单元中。您可能最感兴趣的是 GetPropList,它将返回对象属性的列表,而 GetPropValue 将允许您获取属性的值。

procedure TForm1.DumpObject( YourObjectInstance : tObject );
var
  PropList: PPropList;
  PropCnt: integer;
  iX: integer;
  vValue: Variant;
  sValue: String;
begin
  PropCnt := GetPropList(YourObjectInstance,PropList);
  for iX := 0 to PropCnt-1 do
    begin
      vValue := GetPropValue(YourObjectInstance,PropList[ix].Name,True);
      sValue := VarToStr( vValue );
      Memo1.Lines.Add(PropList[ix].Name+' = '+sValue );
    end;
end;

例如,在主窗体的按钮单击上使用 DumpObject(Self) 运行它,它会将当前窗体的所有属性转储到备忘录中。这只是发布的属性,并且要求主类或者从 TPersistent 继承,或者在对象之前打开 {$M+} 进行编译。

有传言说,在 Delphi 的未来版本(可能是 2010 年)中将提供类似“反射器”的能力。

于 2009-05-28T22:09:58.730 回答
3

Consider something like Codesite which is a much more complete tracing solution. It allows you to output much more complex info, and then search, print, and analyse the data. But for your purposes, you can simply send an object to it with Codesite.Send('Before', self); and you get all the RTTI available properties in the log. Do an "After" one too, and then you can compare the two in the Codesite output just by selecting both. It's saved me many times.

于 2009-05-29T08:25:58.613 回答
-2

如果 delphi 7 是 .NET 版本,那么您可以(其中一些)通过反射来实现。(不容易,但也不是特别难)。如果它是正常的,编译的东西,那么这是一个难题,调试器是你最好的选择,除了专门的打印功能/方法。

于 2009-05-28T21:16:49.587 回答