代码之家  ›  专栏  ›  技术社区  ›  Mike Sutton

Delphi OTA和RTTI错误

  •  3
  • Mike Sutton  · 技术社区  · 17 年前

    我正在写一篇德尔菲专家论文。我需要能够将值写入作为对象的属性上的属性。例如,我在表单上有一个GroupBox,我想编辑边距。Left属性。我使用下面的程序来做这件事,但如果在标记线上给出AV。

    该过程从(属性编辑器)中获取一个组件、属性名称(例如“Margins.Left”)和新值,解析出属性名称,获取对象,读取当前值,并尝试更改它(如果不同)。然后它调用一个方法来记录任何更改。

    procedure EditIntegerSubProperty(Component: IOTAComponent;const PropName: String;NewValue: Integer);
    var AnObject: TObject;
      TK: TTypeKind;
      At: Integer;
      AClassName, APropName: String;
      PropInfo: PPropInfo;
      OldValue: Integer;
    begin
      At := Pos('.', PropName);
      if At < 1 then
      raise Exception.Create('Invalid SubProperty Name: '+PropName);
    
      AClassName := Copy(PropName, 1, At-1);
      APropName := Copy(PropName, At+1, length(PropName));
    
      TK := Component.GetPropTypeByName(AClassName);
      if TK <> tkClass then
        EXIT;
    
      AnObject := GetObjectProp((Component as INTAComponent).GetComponent, AClassName);
      if PropIsType(AnObject, APropName, tkInteger) then
      begin
        OldValue := GetInt64Prop(AnObject, APropName);
        if OldValue <> NewValue then
        begin
          SetInt64Prop(AnObject, APropName, NewValue);  <----AV HERE
          ChangeLogInteger(Name, PropName, OldValue, NewValue);
        end;
      end;
    end;
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Ondrej Kelle    17 年前

    您是否尝试使用GetOrdProp,SetOrdProp而不是GetInt64Prop,SetInt64Prop?

        2
  •  3
  •   Roddy    17 年前

    Margins.xyzy都是整数属性,而不是Int64属性,因此需要使用GetOrdProp/SetOrdProp来读取和修改它们。

    您可以根据PropIsType调用来判断要调用哪些函数。

    • tkInt64:Get/SetInt64Prop
    • tkInteger:Get/SetOrdProp