我有一个记录类型如下:Type 
  TNewRec  = record
    A : String;
    D : Char;
    C : Integer;
    B : String;
    E : TDateTime
  end;在不知道记录子项(item)名称的前提下, 怎样取得子项的名称(如A,B,C,D,E)和对应的数据类型? 能不能做到? 为什么? 最好用贴一下代码.

解决方案 »

  1.   

    To: delphi0build(超级模块)我的用意是这样的:
    假设TnewRec中有100个数据子项(item),修改过后, 我要比较它与修改前有什么不同, 并且把字段名称与修改内容写入历史数据库.
    一般情况是:
    if NewRec1.001 <> NewRec2.002 then ……. if NewRec1.100 <> NewRec2.100 then.
    我不想这样做, 因为代码太长, 而且都是写同一数据库, 内容相当类似.
    我想做一个通用一点的过程.
    For I :=0 to 99
    Begin
      //遍历数据类型的子项并执行过程序比较,写入数据库.
    End;
    这样, 执行过程前就要知道是当前执行NewRec1.001还是NewRec1.100
    有没有更好的建议或解决方法?
      

  2.   

    做一个CLASS,里面定义这样的方法
      

  3.   

    当然可以, 你的意思是指先把当前记录存入数据库. 然后:
    For I := DataSet1.fieldcount -1 do
    begin
      if Dataset1.fields[i] = Dataset2.fields[i] then 
      begin
       .......
      end;
    end;
    ?????
    这样的话, 我要多建一个表. 多一次查询!
    可行, 有没有更好的方法?
      

  4.   

    是这个意思
    我觉得还可用RTTI
    用{$M+}来产生
    但不能用record,要用class,还要published出去
    如下unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      {$M+}
      TNewRec  = class
      private
        FA : String;
        FD : Char;
        FC : Integer;
        FB : String;
        FE : TDateTime;
      published
        property A: string read FA write FA;
        property D: Char read FD write FD;
        property C: Integer read FC write FC;
        property B: string read FB write FB;
        property E: TDateTime read FE write FE;
      end;
      {M-}  TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    uses TypInfo;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      PropList: PPropList;
      PropCount: Integer;
      I: Integer;
    begin
      PropCount := GetPropList(TNewRec.ClassInfo, PropList);
      for I := 0 to PropCount - 1 do
        Memo1.Lines.Add('字段名: ' + PropList[I].Name + '====' +
          '类型: ' +
          Copy(GetEnumName(TypeInfo(TTypeKind), Integer(PropList[I].PropType^.Kind)), 3 , MaxInt));
    end;end.
      

  5.   

    To: xzgyb(老达摩)PropCount := GetPropList(TNewRec.ClassInfo, PropList);
    这句编译不过!
    提示[Error] Unit1.pas(52): Incompatible types: 'TTypeKinds' and 'PPropList'
    我用的是D5, 会不会D5与D7的这个命令参数不一样?
      

  6.   

    在D5下,这样procedure TForm1.Button1Click(Sender: TObject);
    var
      PropList: PPropList;
      PropCount: Integer;
      I: Integer;
    begin
      PropCount := GetTypeData(TNewRec.ClassInfo)^.PropCount;
      GetMem(PropList, PropCount * SizeOf(Pointer));
      try
        GetPropInfos(TNewRec.ClassInfo, PropList);
        for I := 0 to PropCount - 1 do
          Memo1.Lines.Add('字段名: ' + PropList[I].Name + '====' +
            '类型: ' +
            Copy(GetEnumName(TypeInfo(TTypeKind), Integer(PropList[I].PropType^.Kind)), 3 , MaxInt));
      finally
        FreeMem(PropList);
      end;
    end;
      

  7.   

    To: xzgyb(老达摩) 
    大侠!果然高高高高高高高高高高高高高高高高!!!!!!
    谢了!